本文介绍了无法获取@Secured方法安全注解春季安全工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多的研究,对我一切看起来正确的......但我不能得到这个工作!任何人有任何想法?

不管我做什么,相关的映射仍然公开向任何人(匿名或登录,无论他们有什么样的角色)。

我非常希望拥有的所有请求是公开的,除了那些由@Secured(注释) - 明明只有与特定角色的用户将被允许访问这些映射

这可能吗?

FYI作为一种解决方法目前我建造了检查的作用的方法hasRole(字符串角色)已登录的用户,并抛出一个NotAuthorizedException(定制),如果该方法返回false。

的UserDetails

  @覆盖
  公文集&LT ;?扩展的GrantedAuthority> getAuthorities(){      清单<&GrantedAuthority的GT; grantedAuthorities = NULL;      System.out.print(帐户的角色......);
      的System.out.println(account.getRole());      如果(account.getRole()。等于(USER)){
          GrantedAuthority的一个GrantedAuthority =新SimpleGrantedAuthority(ROLE_USER);
          grantedAuthorities = Arrays.asList(的GrantedAuthority);
      }      如果(account.getRole()。等于(ADMIN)){
          的GrantedAuthority grantedAuthorityUser =新SimpleGrantedAuthority(ROLE_USER);
          的GrantedAuthority grantedAuthorityAdmin =新SimpleGrantedAuthority(ROLE_ADMIN);
          grantedAuthorities = Arrays.asList(grantedAuthorityUser,grantedAuthorityAdmin);
      }      返回grantedAuthorities;
  }

SecurityConfig

  @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled =真)
公共类SecurityConfig扩展WebSecurityConfigurerAdapter {    @Autowired
    私人AuthFailure authFailure;    @Autowired
    私人AuthSuccess authSuccess;    @Autowired
    私人EntryPointUnauthorizedHandler unauthorizedHandler;    @Autowired
    私人UserDetailsS​​erviceImpl的UserDetailsS​​ervice;    / * @自动装配
    公共无效configAuthBuilder(AuthenticationManagerBuilder建设者)抛出异常{
        builder.userDetailsS​​ervice(UserDetailsS​​ervice的);
    } * /    @覆盖
    公众的AuthenticationManager authenticationManagerBean()抛出异常{
        返回super.authenticationManagerBean();
    }    @Autowired
    @覆盖
    公共无效配置(AuthenticationManagerBuilder建设者)抛出异常{
        builder.userDetailsS​​ervice(UserDetailsS​​ervice的);
    }  私人CsrfTokenRepository csrfTokenRepository(){
    HttpSessionCsrfTokenRepository库=新HttpSessionCsrfTokenRepository();
    repository.setHeaderName(X-XSRF-TOKEN);
    返回库;
  }    @覆盖
    公共无效配置(HttpSecurity HTTP)抛出异常{
      http.csrf()。csrfTokenRepository(csrfTokenRepository())
        。而()。exceptionHandling()的AuthenticationEntryPoint(unauthorizedHandler)
        。而()。formLogin()。loginPage(/登录)。successHandler(authSuccess).failureHandler(authFailure)
        //和()。authorizeRequests()。antMatchers(/ REST / **)。验证()
        //和()。authorizeRequests()。antMatchers(/ **)。permitAll()
        。而()。addFilterAfter(新CsrfHeaderFilter(),CsrfFilter.class);;
    }

的AccountController

  @Secured(ROLE_USER)
  @RequestMapping(方法= RequestMethod.GET)
  公开名单<&帐户GT; getAllAccounts(@RequestParam(值=邮件,要求= FALSE)字符串的邮件){

谢谢!


解决方案

您可以使用控制器范围的安全与Spring HttpSecurity。尝试添加到您的配置方法:

  .antMatchers(休息/账户*)。hasRole(ADMIN)

如果你希望的任何请求是公共的(真的吗?)

  .anyRequest()。permitAll()

您还可以保护您的MethodInvocation例在你的UserDetailsS​​ervice当您从任何地方访问它:

  @Secured(ROLE_USER)
公共getAllAccounts(...){...}

只有这样,你必须注释你的SecurityConfig:

  @EnableGlobalMethodSecurity(securedEnabled =真)

see: http://docs.spring.io/autorepo/docs/spring-security/4.0.0.CI-SNAPSHOT/reference/htmlsingle/#request-matching

这篇关于无法获取@Secured方法安全注解春季安全工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:39