尚硅谷Security笔记-1

设置用户权限

有四种方法。

1.hasAuthority() 方法:只能给用户一个权限

@Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/login.html") // 自定义 login 页面
                .loginProcessingUrl("/user/login") // 登录的处理URL(controller)
                .defaultSuccessUrl("/test/home").permitAll() // 登录成功后调转的页面
                .and()
                .authorizeRequests() // 需要设置权限的请求
                .antMatchers("/", "/test/hello", "/user/login").permitAll() // 不需要过滤的资源
                // 1. hasAuthority() 只能给用户一个权限
                .antMatchers("/test/home").hasAuthority("admin")
                .anyRequest().authenticated() // 任何请求都可以访问
                .and()
                .csrf().disable();
    }

2.hasAnyAuthority() 方法:可以给多个权限

@Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/user/login")
                .defaultSuccessUrl("/test/home").permitAll()
                .and()
                .authorizeRequests()
                .antMatchers("/", "/test/hello", "/user/login").permitAll()
                // 2. hasAnyAuthority() 可以给多个权限
                .antMatchers("/test/home").hasAnyAuthority("admin,manager")
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }

3.hasRole()方法:只能给一个权限。

而且底层方法中,会给权限名加上【ROLE_】前缀。

@Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/user/login")
                .defaultSuccessUrl("/test/home").permitAll()
                .and()
                .authorizeRequests()
                .antMatchers("/", "/test/hello", "/user/login").permitAll()
                // 3. hasRole()
                .antMatchers("/test/home").hasRole("sale")
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }

注意 hasRole(“sale”) 的参数是sale下面代码的参数则是 ROLE_sale 才能成功运行

@Service("userDetailsService")
public class  CustomerUserDetailService implements UserDetailsService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        // 根据用户名查询
        wrapper.eq("username", username);
        // selectOne 返回一条结果
        Users user = usersMapper.selectOne(wrapper);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_sale");
        return new User(user.getUsername(), new BCryptPasswordEncoder().encode(user.getPassword()), auths);
    }
}

注意上面倒数第二行代码 AuthorityUtils.commaSeparatedStringToAuthorityList(“admin,ROLE_sale”);

4.hasAnyRole()

还有一种方法是 hasAnyRole() 和 hasAnyAuthority() 差不多,不写了,用的时候百度。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tzzt01

您的支持是我坚持下去的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值