1111import org .springframework .context .annotation .Configuration ;
1212import org .springframework .http .HttpMethod ;
1313import org .springframework .security .access .vote .UnanimousBased ;
14+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
15+ import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
1416import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1517import org .springframework .security .config .annotation .web .builders .WebSecurity ;
1618import org .springframework .security .config .annotation .web .builders .WebSecurity .IgnoredRequestConfigurer ;
1719import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1820import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
1921import org .springframework .security .config .http .SessionCreationPolicy ;
22+ import org .springframework .security .core .userdetails .UserDetailsService ;
2023import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
2124import org .springframework .security .crypto .password .PasswordEncoder ;
2225import org .springframework .security .web .access .expression .WebExpressionVoter ;
2326import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
2427import top .cadecode .sra .common .enums .AuthModelEnum ;
28+ import top .cadecode .sra .framework .security .LoginSuccessHandler ;
2529import top .cadecode .sra .framework .security .TokenAuthFilter ;
26- import top .cadecode .sra .framework .security .handler .*;
30+ import top .cadecode .sra .framework .security .handler .LoginFailureHandler ;
31+ import top .cadecode .sra .framework .security .handler .NoAuthenticationHandler ;
32+ import top .cadecode .sra .framework .security .handler .NoAuthorityHandler ;
33+ import top .cadecode .sra .framework .security .handler .SignOutSuccessHandler ;
34+ import top .cadecode .sra .framework .security .voter .DataBaseRoleVoter ;
2735
2836import java .util .Arrays ;
2937import java .util .List ;
30- import java .util .Objects ;
31- import java .util .stream .Collectors ;
3238
3339/**
3440 * @author Cade Li
3945@ Data
4046@ RequiredArgsConstructor
4147@ EnableWebSecurity
48+ @ EnableGlobalMethodSecurity (prePostEnabled = true )
4249@ Configuration
4350@ ConfigurationProperties ("sra.security" )
44- @ ConditionalOnProperty (name = "sra.config.security-on" , havingValue = "true" )
4551public class SecurityConfig {
4652
4753 /**
@@ -76,14 +82,38 @@ public class SecurityConfig {
7682 /**
7783 * 注入 Token 过滤器
7884 */
79- private final List < TokenAuthFilter > tokenAuthFilters ;
85+ private final TokenAuthFilter tokenAuthFilter ;
8086
8187 /**
82- * security 配置
88+ * 注入投票器
89+ */
90+ private final DataBaseRoleVoter dataBaseRoleVoter ;
91+
92+ /**
93+ * 注入 UserDetailsService
94+ */
95+ private final UserDetailsService userDetailsService ;
96+
97+ /**
98+ * 密码加密器
99+ */
100+ @ Bean
101+ public PasswordEncoder passwordEncoder () {
102+ return new BCryptPasswordEncoder ();
103+ }
104+
105+ /**
106+ * Security 配置
83107 */
84108 @ Bean
85- public WebSecurityConfigurerAdapter webSecurityConfigurer () {
109+ public WebSecurityConfigurerAdapter webSecurityConfigurer (PasswordEncoder passwordEncoder ) {
86110 return new WebSecurityConfigurerAdapter () {
111+
112+ @ Override
113+ protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
114+ auth .userDetailsService (userDetailsService ).passwordEncoder (passwordEncoder );
115+ }
116+
87117 @ Override
88118 protected void configure (HttpSecurity http ) throws Exception {
89119 // 关闭 csrf
@@ -95,11 +125,6 @@ protected void configure(HttpSecurity http) throws Exception {
95125 // 尝试请求直接通过
96126 .antMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
97127 .anyRequest ().authenticated ();
98- // 配置登录处理器
99- http .formLogin ().permitAll ()
100- .loginProcessingUrl (LOGIN_URL )
101- .successHandler (loginSuccessHandler )
102- .failureHandler (loginFailureHandler );
103128 // 配置注销处理器
104129 http .logout ().permitAll ()
105130 .logoutUrl (LOGOUT_URL )
@@ -109,24 +134,18 @@ protected void configure(HttpSecurity http) throws Exception {
109134 // 配置未登录处理器
110135 .authenticationEntryPoint (noAuthenticationHandler )
111136 // 配置无权限处理器
112- .accessDeniedHandler (noAuthorityHandler )
113- .and ()
114- // 自定义的 accessDecisionManager
115- .authorizeRequests ()
137+ .accessDeniedHandler (noAuthorityHandler );
138+ // 自定义的 accessDecisionManager
139+ http .authorizeRequests ()
116140 .accessDecisionManager (new UnanimousBased (Arrays .asList (new WebExpressionVoter ())));
117- // 根据认证模式配置过滤器
118- if ( Objects . isNull ( authModel )) {
119- authModel = AuthModelEnum . JWT ;
120- log . info ( "没有配置认证模式,默认为 {}" , authModel );
121- }
141+ // 配置登录处理器
142+ http . formLogin (). permitAll ()
143+ . loginProcessingUrl ( LOGIN_URL )
144+ . successHandler ( loginSuccessHandler )
145+ . failureHandler ( loginFailureHandler );
122146 // 配置 Token 校验过滤器
123- List <TokenAuthFilter > filters = tokenAuthFilters .stream ()
124- .filter (o -> o .getAuthModel () == authModel )
125- .collect (Collectors .toList ());
126- if (!filters .isEmpty ()) {
127- http .addFilterBefore (filters .get (0 ), UsernamePasswordAuthenticationFilter .class );
128- log .info ("完成 Security 配置,认证模式 {}" , authModel );
129- }
147+ http .addFilterBefore (tokenAuthFilter , UsernamePasswordAuthenticationFilter .class );
148+ log .info ("完成 Security 配置,认证模式 {}" , authModel );
130149 }
131150
132151 @ Override
@@ -143,13 +162,4 @@ public void configure(WebSecurity web) {
143162 }
144163 };
145164 }
146-
147- /**
148- * 密码加密器
149- */
150- @ Bean
151- public PasswordEncoder passwordEncoder () {
152- return new BCryptPasswordEncoder ();
153- }
154-
155165}
0 commit comments