关于Spring Boot下Spring Security权限访问设置@PreAuthorize(“hasRole(‘ROLE_ADMIN‘)“)没有用

承接上篇:Spring Security整合后post数据不了,403拒绝访问
前几天想要限制不同角色的访问权限,于是就直接使用:

@PreAuthorize("hasRole('ROLE_ADMIN')")

注解来标注一个实现类的方法上,但是其他权限依然可以访问 orz,于是我怀疑是放的位置不对,于是放在了Service接口里的方法上,也未果。于是直接放在Controller层的访问方法上,还是未果 ==|||

好了,上网查了一番:Spring Security @PreAuthorize 拦截无效
这篇文章刚好戳中要点:
没有设置开启prePostEnable=true;因为这个默认为false;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    @Bean
    UserDetailsService customUserService() {
        return new CustomUserService();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService());
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/test","/test1").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                    .loginPage(RequestUrls.LoginUrl)
                    .failureUrl(RequestUrls.LoginUrl+"?error")
                    .permitAll()
                .and()
                .logout().permitAll();
    }
}

如上,添加:

@EnableGlobalMethodSecurity(prePostEnabled=true)

 

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

以及:

    @Autowired//注意这个方法是注入的
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService());
    }

就酱紫~权限访问完美解决了!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用Spring Security来实现权限控制。下面是一个简单的示例: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 配置安全设置 在`application.properties`文件中添加以下配置: ``` # 禁用CSRF保护 spring.security.csrf.enabled=false # 配置用户及其角色 spring.security.user.name=user spring.security.user.password=password spring.security.user.roles=USER # 配置其他角色 security.roles.admin=ROLE_ADMIN security.roles.user=ROLE_USER ``` 3. 定义访问控制规则 在`WebSecurityConfigurerAdapter`类中定义访问控制规则,如下所示: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin() .and() .logout(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user") .password("{noop}password") .roles("USER") .and() .withUser("admin") .password("{noop}password") .roles("ADMIN"); } } ``` 在上面的配置中,我们定义了两个角色:`ADMIN`和`USER`,并分别为它们指定了访问控制规则。具体来说,访问`/admin/**`的请求需要`ADMIN`角色,访问`/user/**`的请求需要`USER`角色,其他请求需要身份验证。 4. 使用注解控制访问 在Controller层中可以使用注解来控制访问,示例代码如下: ``` @RestController @RequestMapping("/admin") @PreAuthorize("hasRole('ADMIN')") public class AdminController { @GetMapping("/hello") public String hello() { return "Hello, admin!"; } } @RestController @RequestMapping("/user") @PreAuthorize("hasRole('USER')") public class UserController { @GetMapping("/hello") public String hello() { return "Hello, user!"; } } ``` 在上面的代码中,我们使用`@PreAuthorize`注解来控制访问,指定了需要`ADMIN`或`USER`角色才能访问

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值