多个HttpSecurity的配置

/**
 * @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();
        }
    }



}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值