|
@ -1,10 +1,12 @@ |
|
|
package com.aiprose.scauth.conf; |
|
|
package com.aiprose.scauth.conf; |
|
|
|
|
|
|
|
|
import com.aiprose.scauth.entity.User; |
|
|
import com.aiprose.scauth.entity.User; |
|
|
|
|
|
import com.aiprose.scauth.filter.JWTAuthenticationFilter; |
|
|
import com.aiprose.scauth.handler.*; |
|
|
import com.aiprose.scauth.handler.*; |
|
|
import com.aiprose.scauth.service.IUserService; |
|
|
import com.aiprose.scauth.service.IUserService; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
import org.springframework.context.annotation.Configuration; |
|
|
import org.springframework.context.annotation.Configuration; |
|
|
import org.springframework.security.access.AccessDecisionManager; |
|
|
import org.springframework.security.access.AccessDecisionManager; |
|
|
import org.springframework.security.access.AccessDecisionVoter; |
|
|
import org.springframework.security.access.AccessDecisionVoter; |
|
@ -17,11 +19,15 @@ import org.springframework.security.config.annotation.method.configuration.Enabl |
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
|
import org.springframework.security.config.annotation.web.builders.WebSecurity; |
|
|
import org.springframework.security.config.annotation.web.builders.WebSecurity; |
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy; |
|
|
import org.springframework.security.core.userdetails.UserDetailsService; |
|
|
import org.springframework.security.core.userdetails.UserDetailsService; |
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
|
|
import org.springframework.security.web.access.expression.WebExpressionVoter; |
|
|
import org.springframework.security.web.access.expression.WebExpressionVoter; |
|
|
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; |
|
|
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; |
|
|
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
@ -34,7 +40,7 @@ import java.util.List; |
|
|
* @since 1.0 |
|
|
* @since 1.0 |
|
|
*/ |
|
|
*/ |
|
|
@Configuration |
|
|
@Configuration |
|
|
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true) |
|
|
//@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true)
|
|
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private IUserService userService; |
|
|
private IUserService userService; |
|
@ -45,21 +51,23 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
@Override |
|
|
@Override |
|
|
public void configure(WebSecurity web) throws Exception { |
|
|
public void configure(WebSecurity web) throws Exception { |
|
|
// super.configure(web);
|
|
|
// super.configure(web);
|
|
|
|
|
|
web.ignoring().antMatchers("login", "/v2/api-docs/**", "/swagger-resources/**", "/swagger-ui.html"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
http.csrf().disable(); |
|
|
http.cors().and().csrf().disable(); |
|
|
|
|
|
|
|
|
// 配置记住我的参数和记住我处理类
|
|
|
// 授权配置
|
|
|
http.rememberMe() |
|
|
http.authorizeRequests().anyRequest().authenticated(); |
|
|
.tokenRepository(remeberMeHandler) |
|
|
|
|
|
.tokenValiditySeconds(60*60*24) |
|
|
|
|
|
.userDetailsService(userDetailsService()); |
|
|
|
|
|
|
|
|
|
|
|
// 配置登录页面
|
|
|
// 配置登录
|
|
|
http.formLogin().loginPage("/login").permitAll(); |
|
|
http.formLogin().usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login"); |
|
|
|
|
|
|
|
|
|
|
|
//登录过期、 未登录
|
|
|
|
|
|
http.exceptionHandling().authenticationEntryPoint(new LoginExpireHandler()); |
|
|
|
|
|
// 配置登录失败后的操作
|
|
|
|
|
|
http.formLogin().failureHandler(new LoginFailureHandler()); |
|
|
// 配置登录成功后的操作
|
|
|
// 配置登录成功后的操作
|
|
|
http.formLogin().successHandler(new LoginSuccessHandler()); |
|
|
http.formLogin().successHandler(new LoginSuccessHandler()); |
|
|
|
|
|
|
|
@ -67,33 +75,27 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
http.exceptionHandling().accessDeniedHandler(new AuthLimitHandler()); |
|
|
http.exceptionHandling().accessDeniedHandler(new AuthLimitHandler()); |
|
|
|
|
|
|
|
|
// 登出授权
|
|
|
// 登出授权
|
|
|
http.logout().permitAll(); |
|
|
// http.logout().permitAll();
|
|
|
|
|
|
|
|
|
// 授权配置
|
|
|
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
|
|
http.authorizeRequests() |
|
|
|
|
|
/* 所有静态文件可以访问 */ |
|
|
/* 配置token验证过滤器 */ |
|
|
.antMatchers("/js/**","/css/**","/images/**").permitAll() |
|
|
http.addFilterBefore(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); |
|
|
/* 所有 以/ad 开头的 广告页面可以访问 */ |
|
|
|
|
|
.antMatchers("/ad/**").permitAll() |
|
|
|
|
|
.antMatchers("/user/**","/role/**").permitAll() |
|
|
|
|
|
/* 动态url权限 */ |
|
|
|
|
|
.withObjectPostProcessor(new DefinedObjectPostProcessor()) |
|
|
|
|
|
/* url决策 */ |
|
|
|
|
|
.accessDecisionManager(accessDecisionManager()) |
|
|
|
|
|
.anyRequest().authenticated(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
|
|
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder()); |
|
|
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder()); |
|
|
// auth.passwordEncoder(new BCryptPasswordEncoder());
|
|
|
} |
|
|
// .withUser("nelson").password(new BCryptPasswordEncoder().encode("123456")).roles("admin")
|
|
|
|
|
|
// .and()
|
|
|
@Bean |
|
|
// .withUser("yasaka").password(new BCryptPasswordEncoder().encode("123456")).roles("user")
|
|
|
public WebMvcConfigurer corsConfigurer() { |
|
|
// .and()
|
|
|
return new WebMvcConfigurer() { |
|
|
// .withUser("one").password(new BCryptPasswordEncoder().encode("123456")).roles("gest")
|
|
|
@Override |
|
|
// .and()
|
|
|
public void addCorsMappings(CorsRegistry registry) { |
|
|
// .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("root");
|
|
|
registry.addMapping("/**"); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -117,25 +119,25 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
* |
|
|
* |
|
|
* 决策管理 |
|
|
* 决策管理 |
|
|
*/ |
|
|
*/ |
|
|
private AccessDecisionManager accessDecisionManager() { |
|
|
// private AccessDecisionManager accessDecisionManager() {
|
|
|
List<AccessDecisionVoter<? extends Object>> decisionVoters = new ArrayList<>(); |
|
|
// List<AccessDecisionVoter<? extends Object>> decisionVoters = new ArrayList<>();
|
|
|
decisionVoters.add(new WebExpressionVoter()); |
|
|
// decisionVoters.add(new WebExpressionVoter());
|
|
|
decisionVoters.add(new AuthenticatedVoter()); |
|
|
// decisionVoters.add(new AuthenticatedVoter());
|
|
|
decisionVoters.add(new RoleVoter()); |
|
|
// decisionVoters.add(new RoleVoter());
|
|
|
/* 路由权限管理 */ |
|
|
// /* 路由权限管理 */
|
|
|
decisionVoters.add(new UrlRoleAuthHandler()); |
|
|
// decisionVoters.add(new UrlRoleAuthHandler());
|
|
|
return new UnanimousBased(decisionVoters); |
|
|
// return new UnanimousBased(decisionVoters);
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
// @Autowired
|
|
|
private UrlRolesFilterHandler urlRolesFilterHandler; |
|
|
// private UrlRolesFilterHandler urlRolesFilterHandler;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DefinedObjectPostProcessor implements ObjectPostProcessor<FilterSecurityInterceptor> { |
|
|
// class DefinedObjectPostProcessor implements ObjectPostProcessor<FilterSecurityInterceptor> {
|
|
|
@Override |
|
|
// @Override
|
|
|
public <O extends FilterSecurityInterceptor> O postProcess(O object) { |
|
|
// public <O extends FilterSecurityInterceptor> O postProcess(O object) {
|
|
|
object.setSecurityMetadataSource(urlRolesFilterHandler); |
|
|
// object.setSecurityMetadataSource(urlRolesFilterHandler);
|
|
|
return object; |
|
|
// return object;
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
} |
|
|