Spring Security 入门 权限表达式控制URL权限

配置资源授权,默认是不需要授权。现在访问/say是要有权限aa的

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login/page")
                .loginProcessingUrl("/login")
                .and()
                .authorizeRequests()
                .antMatchers("/login/page").permitAll()
                .anyRequest().authenticated()
                //以上是认证的配置,以下是授权的配置
                //拥有 aa 权限的用户,可以访问任意请求方式的 /say
                .antMatchers("/say").hasAuthority("aa")
        ;
        http.csrf().disable();
    }
}

给devin用户的权限是ADMIN,并么有aa权限,因此/say是没有权限访问的。

@Slf4j
@Component
public class CustomUserDetailService implements UserDetailsService {

    @Autowired
    public PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //模拟用户可以在数据库查询到
        if ("devin".equals(username)) {
            return new User("devin", passwordEncoder.encode("1234"),
                    //这个给的权限标识符为 ADMIN
                    AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
        }
        throw new UsernameNotFoundException("用户名输入错误");
    }
}

添加/say

@RestController
public class HelloController {

    @RequestMapping("hello")
    public String hell() {
        return "Hello World";
    }

    @RequestMapping("say")
    public String say() {
        return "say";
    }
}

登录访问:http://localhost:8080/hello
在这里插入图片描述
访问:http://localhost:8080/say 没有权限
在这里插入图片描述


@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login/page")
                .loginProcessingUrl("/login")
                .and()
                .authorizeRequests()
                .antMatchers("/login/page").permitAll()
                //拥有 aa 权限的用户,可以访问任意请求方式的 /say
                .antMatchers("/say").hasAuthority("aa") //配置权限
                .antMatchers("/run").hasRole("ADMIN") //配置角色
                .anyRequest().authenticated()
        ;
        http.csrf().disable();
    }
}
@Slf4j
@Component
public class CustomUserDetailService implements UserDetailsService {

    @Autowired
    public PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //模拟用户可以在数据库查询到
        if ("devin".equals(username)) {
            return new User("devin", passwordEncoder.encode("1234"),
                    //这个给的权限标识符为 aa, 角色为ROLE_ADMIN(这里要注意加上前缀ROLE_标识是角色)
                    AuthorityUtils.commaSeparatedStringToAuthorityList("aa, ROLE_ADMIN"));
        }
        throw new UsernameNotFoundException("用户名输入错误");
    }
}

登录访问:http://localhost:8080/hello
在这里插入图片描述

登录访问:http://localhost:8080/say
在这里插入图片描述

登录访问:http://localhost:8080/run
在这里插入图片描述


可参考的权限表达式
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值