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

View File

@ -5,14 +5,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 监控服务
* @author zyf
* @date: 2022/4/21 10:55
* @author scott
*/
@SpringBootApplication
@EnableAdminServer
public class JeecgMonitorApplication {
public static void main(String[] 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.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
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
@ -15,49 +19,43 @@ import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@Configuration
public class SecuritySecureConfig {
private final String adminContextPath;
private final AdminServerProperties adminServer;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
this.adminServer = adminServerProperties;
}
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
// 登录成功处理类
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http
// authorizeRequests() 已废弃,使用 authorizeHttpRequests()
.authorizeHttpRequests(authorize -> authorize
//静态文件允许访问
.requestMatchers(adminContextPath + "/assets/**").permitAll()
//登录页面允许访问
.requestMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
//其他所有请求需要登录
.anyRequest().authenticated()
public SecurityWebFilterChain configure(ServerHttpSecurity http) {
return http
.authorizeExchange(authorize -> authorize
.pathMatchers(adminServer.path("/assets/**")).permitAll()
.pathMatchers(adminServer.path("/login")).permitAll()
.pathMatchers("/actuator/health/**").permitAll()
.anyExchange().authenticated()
)
//登录页面配置用于替换security默认页面
.formLogin(formLogin -> formLogin
.loginPage(adminContextPath + "/login")
.successHandler(successHandler)
.loginPage(adminServer.path("/login"))
.authenticationSuccessHandler(loginSuccessHandler(adminServer.path("/")))
)
//登出页面配置用于替换security默认页面
.logout(logout -> logout
.logoutUrl(adminContextPath + "/logout")
.logoutUrl(adminServer.path("/logout"))
.logoutSuccessHandler(logoutSuccessHandler(adminServer.path("/login?logout")))
)
.httpBasic(Customizer.withDefaults())
.csrf(csrf -> csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
"/instances",
"/actuator/**"
)
);
return http.build();
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.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;
}
}