package pers.lbw.digitalmall.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.util.AntPathMatcher;
import javax.servlet.annotation.MultipartConfig;
@EnableWebSecurity
@Configuration
public class MultiHttpSecurityConfig{
@Configuration
@Order(1)
public static class ForeConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/fore/**")//多HttpSecurity配置时必须设置这个,除最后一个外,因为不设置的话默认匹配所有,就不会执行到下面的HttpSecurity了
.formLogin()
.loginPage("/fore/user/login")//登陆界面页面跳转URL
.loginProcessingUrl("/fore/user/loginPost")//登陆界面发起登陆请求的URL
.failureUrl("/fore/user/login")//登陆失败的页面跳转URL
.permitAll()//表单登录,permitAll()表示这个不需要验证
.and()//Return the SecurityBuilder
.authorizeRequests()//启用基于 HttpServletRequest 的访问限制,开始配置哪些URL需要被保护、哪些不需要被保护
.antMatchers("/user/**", "/detail/toDetailPage*").permitAll()//未登陆用户允许的请求
.anyRequest().hasAnyRole("USER")//其他/fore路径下的请求全部需要登陆,获得USER角色
.and()
.csrf().disable();
}
}
@Configuration
@Order(2)
public static class AdminSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/admin/**")
.formLogin()
.loginPage("/fore/user/login")//登陆界面页面跳转URL
.loginProcessingUrl("/fore/user/login111")//登陆界面发起登陆请求的URL
.failureUrl("/fore/user/login")//登陆失败的页面跳转URL
.permitAll()//表单登录,permitAll()表示这个不需要验证
.and()//Return the SecurityBuilder
.authorizeRequests()//启用基于 HttpServletRequest 的访问限制,开始配置哪些URL需要被保护、哪些不需要被保护
.antMatchers("/admin/**").hasAnyRole("ADMIN")//其他/fore路径下的请求全部需要登陆,获得USER角色
.and()
.csrf().disable();
}
}
@Configuration
@Order(3)
public static class OtherSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()//启用基于 HttpServletRequest 的访问限制,开始配置哪些URL需要被保护、哪些不需要被保护
.antMatchers("/","/code/**","/css/**", "/img/**", "/js/**").permitAll()//其他请求放行
.and()
.csrf()
.disable();//未登陆用户允许的请求
}
}
}
SpringSecurity配置多个HttpSecurity(SpringBoot适用)
最新推荐文章于 2024-10-15 08:00:00 发布