架构升级springboot4

This commit is contained in:
JEECG 2026-07-07 21:56:22 +08:00
parent fe26227ce5
commit 68d4b2d24c
3 changed files with 42 additions and 42 deletions

View File

@ -19,7 +19,7 @@
<dependency> <dependency>
<groupId>de.codecentric</groupId> <groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId> <artifactId>spring-boot-admin-starter-server</artifactId>
<version>3.5.2</version> <version>4.1.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
@ -59,6 +59,9 @@
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.jeecg.monitor.JeecgMonitorApplication</mainClass>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
<resources> <resources>

View File

@ -5,14 +5,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* 监控服务 * @author scott
* @author zyf
* @date: 2022/4/21 10:55
*/ */
@SpringBootApplication @SpringBootApplication
@EnableAdminServer @EnableAdminServer
public class JeecgMonitorApplication { public class JeecgMonitorApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(JeecgMonitorApplication.class, args); SpringApplication.run(JeecgMonitorApplication.class, args);
} }
} }

View File

@ -4,10 +4,14 @@ import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer; import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository; import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
import org.springframework.security.web.server.authentication.logout.RedirectServerLogoutSuccessHandler;
import org.springframework.security.web.server.authentication.logout.ServerLogoutSuccessHandler;
import java.net.URI;
/** /**
* @author scott * @author scott
@ -15,49 +19,43 @@ import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@Configuration @Configuration
public class SecuritySecureConfig { public class SecuritySecureConfig {
private final String adminContextPath; private final AdminServerProperties adminServer;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) { public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath(); this.adminServer = adminServerProperties;
} }
@Bean @Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception { public SecurityWebFilterChain configure(ServerHttpSecurity http) {
// 登录成功处理类 return http
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); .authorizeExchange(authorize -> authorize
successHandler.setTargetUrlParameter("redirectTo"); .pathMatchers(adminServer.path("/assets/**")).permitAll()
successHandler.setDefaultTargetUrl(adminContextPath + "/"); .pathMatchers(adminServer.path("/login")).permitAll()
.pathMatchers("/actuator/health/**").permitAll()
http .anyExchange().authenticated()
// authorizeRequests() 已废弃,使用 authorizeHttpRequests()
.authorizeHttpRequests(authorize -> authorize
//静态文件允许访问
.requestMatchers(adminContextPath + "/assets/**").permitAll()
//登录页面允许访问
.requestMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
//其他所有请求需要登录
.anyRequest().authenticated()
) )
//登录页面配置用于替换security默认页面
.formLogin(formLogin -> formLogin .formLogin(formLogin -> formLogin
.loginPage(adminContextPath + "/login") .loginPage(adminServer.path("/login"))
.successHandler(successHandler) .authenticationSuccessHandler(loginSuccessHandler(adminServer.path("/")))
) )
//登出页面配置用于替换security默认页面
.logout(logout -> logout .logout(logout -> logout
.logoutUrl(adminContextPath + "/logout") .logoutUrl(adminServer.path("/logout"))
.logoutSuccessHandler(logoutSuccessHandler(adminServer.path("/login?logout")))
) )
.httpBasic(Customizer.withDefaults()) .httpBasic(Customizer.withDefaults())
.csrf(csrf -> csrf .csrf(ServerHttpSecurity.CsrfSpec::disable)
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .build();
.ignoringRequestMatchers(
"/instances",
"/actuator/**"
)
);
return http.build();
} }
} private ServerAuthenticationSuccessHandler loginSuccessHandler(String uri) {
RedirectServerAuthenticationSuccessHandler handler = new RedirectServerAuthenticationSuccessHandler();
handler.setLocation(URI.create(uri));
return handler;
}
private ServerLogoutSuccessHandler logoutSuccessHandler(String uri) {
RedirectServerLogoutSuccessHandler handler = new RedirectServerLogoutSuccessHandler();
handler.setLogoutSuccessUrl(URI.create(uri));
return handler;
}
}