🔧 权限属性配置单独放在配置文件中
This commit is contained in:
parent
34913542c0
commit
94f91c9157
|
@ -1,9 +1,10 @@
|
||||||
package cn.bunny.services.security.config;
|
package cn.bunny.services.security.config;
|
||||||
|
|
||||||
|
import cn.bunny.services.security.config.property.AuthorityProperty;
|
||||||
import cn.bunny.services.security.handelr.SecurityAccessDeniedHandler;
|
import cn.bunny.services.security.handelr.SecurityAccessDeniedHandler;
|
||||||
import cn.bunny.services.security.handelr.SecurityAuthenticationEntryPoint;
|
import cn.bunny.services.security.handelr.SecurityAuthenticationEntryPoint;
|
||||||
import cn.bunny.services.security.service.CustomAuthorizationManagerServiceImpl;
|
import cn.bunny.services.security.service.CustomAuthorizationManagerServiceImpl;
|
||||||
import jakarta.annotation.Resource;
|
import lombok.RequiredArgsConstructor;
|
||||||
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.annotation.method.configuration.EnableMethodSecurity;
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||||
|
@ -17,23 +18,28 @@ import org.springframework.security.web.util.matcher.RegexRequestMatcher;
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@EnableMethodSecurity
|
@EnableMethodSecurity
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class WebSecurityConfig {
|
public class WebSecurityConfig {
|
||||||
|
|
||||||
// 需要排出的无需验证的请求路径
|
// 需要排出的无需验证的请求路径
|
||||||
public static String[] annotations = {
|
// public static String[] annotations = {
|
||||||
"/", "/ws/**", "/**.html", "/error",
|
// "/" , "/ws/**" , "/**.html" , "/error" ,
|
||||||
"/media.ico", "/favicon.ico", "/webjars/**", "/v3/api-docs/**", "/swagger-ui/**",
|
// "/media.ico" , "/favicon.ico" , "/webjars/**" , "/v3/api-docs/**" , "/swagger-ui/**" ,
|
||||||
"/*/*/login", "/*/local-file/**", "/*/*/public/**",
|
// "/*/*/login" , "/*/local-file/**" , "/*/*/public/**" ,
|
||||||
};
|
// };
|
||||||
|
public static String[] annotations;
|
||||||
|
|
||||||
// 用户登录之后才能访问,不能与接口名称重复!!!不能与接口名称包含!!!
|
// 用户登录之后才能访问,不能与接口名称重复!!!不能与接口名称包含!!!
|
||||||
public static String[] userAuths = {"private"};
|
// public static String[] userAuths = {"private"};
|
||||||
|
public static String[] userAuths;
|
||||||
|
|
||||||
@Resource
|
private final AuthorityProperty authorityProperty;
|
||||||
private CustomAuthorizationManagerServiceImpl customAuthorizationManagerService;
|
private final CustomAuthorizationManagerServiceImpl customAuthorizationManagerService;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
||||||
|
annotations = authorityProperty.getAnnotations();
|
||||||
|
userAuths = authorityProperty.getUserAuths();
|
||||||
|
|
||||||
httpSecurity
|
httpSecurity
|
||||||
// 前端段分离不需要---禁用明文验证
|
// 前端段分离不需要---禁用明文验证
|
||||||
|
@ -55,7 +61,7 @@ public class WebSecurityConfig {
|
||||||
.rememberMe(AbstractHttpConfigurer::disable)
|
.rememberMe(AbstractHttpConfigurer::disable)
|
||||||
.authorizeHttpRequests(authorize -> authorize
|
.authorizeHttpRequests(authorize -> authorize
|
||||||
.requestMatchers(annotations).permitAll()
|
.requestMatchers(annotations).permitAll()
|
||||||
.requestMatchers(RegexRequestMatcher.regexMatcher(".*\\.(css|js)$")).permitAll()
|
.requestMatchers(RegexRequestMatcher.regexMatcher(".*\\.(css|js)$" )).permitAll()
|
||||||
.anyRequest().access(customAuthorizationManagerService)
|
.anyRequest().access(customAuthorizationManagerService)
|
||||||
)
|
)
|
||||||
.exceptionHandling(exception -> {
|
.exceptionHandling(exception -> {
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package cn.bunny.services.security.config.property;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "auth" )
|
||||||
|
public class AuthorityProperty {
|
||||||
|
|
||||||
|
/* 需要排出的无需验证的请求路径 */
|
||||||
|
private String[] annotations;
|
||||||
|
|
||||||
|
/* 用户登录之后才能访问,不能与接口名称重复和包含!!! */
|
||||||
|
private String[] userAuths;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
auth:
|
||||||
|
annotations:
|
||||||
|
- /
|
||||||
|
- /ws/**
|
||||||
|
- /**.html
|
||||||
|
- /error
|
||||||
|
- /media.ico
|
||||||
|
- /favicon.ico
|
||||||
|
- /webjars/**
|
||||||
|
- /v3/api-docs/**
|
||||||
|
- /swagger-ui/**
|
||||||
|
- /*/*/login
|
||||||
|
- /*/local-file/**
|
||||||
|
- /*/*/public/**
|
||||||
|
user-auths:
|
||||||
|
- private
|
|
@ -6,6 +6,8 @@ server:
|
||||||
spring:
|
spring:
|
||||||
profiles:
|
profiles:
|
||||||
active: @profiles.active@
|
active: @profiles.active@
|
||||||
|
include:
|
||||||
|
- auth
|
||||||
# main:
|
# main:
|
||||||
# lazy-initialization: true
|
# lazy-initialization: true
|
||||||
application:
|
application:
|
||||||
|
|
Loading…
Reference in New Issue