/** * @Author fei * @Date 2021/1/3 2:17 下午 * 因为这边是配置的多个HttpSecurity,所以就不需要再继承 WebSecurityConfigurerAdapter 了 */ @Configuration public class MultiHttpSecurityConfig { /** * 同样这边还是需要密码的编译器 * @return */ @Bean PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Autowired protected void configure(AuthenticationManagerBuilder auth) throws Exception { //在内存中配置密码 auth.inMemoryAuthentication() //以后数据库中便是用这样的密码,不用维护盐字段,很方便。 .withUser("ppf").password("$2a$10$LqCUqjrT4nH1tslRskGQU.CrXOj62vDCZacBanYPBLfVv.h1fOZM2").roles("admin") .and() .withUser("ask").password("$2a$10$X4wfb71Wod6t4LNZlr42.e9cZHiaOs5hdHey8oDPBp/pKNkIwYP/2").roles("user"); } //接下来开始定义静态内部类 @Configuration @Order(1) //@Order标记定义了组件的加载顺序,值越小拥有越高的优先级 public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http.antMatcher("/admin/**").authorizeRequests().anyRequest().hasAnyRole("admin");//匹配权限设置 } } @Configuration //这边不加order注解,优先级肯定是最低的 public static class OtherSecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated() //表示其他没有需要授权的地址,登录之后就能访问。 .and() .formLogin() .loginProcessingUrl("/doLogin") //表示处理登录请求的url .permitAll() //这个表示和登录相关的接口 直接就可以访问。 .and() .csrf().disable(); } } }
多个HttpSecurity的配置
最新推荐文章于 2023-07-25 22:31:29 发布