2017-05-20 22 views
3

私は基本的なSpringBootアプリケーションを持っています。 Spring Initializer、組み込みTomcat、Thymeleafテンプレートエンジン、およびパッケージを実行可能なJARファイルとして使用しています。SpringBootアプリケーションで@RolesAllowedを使用している例外

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
... 
} 

companyServiceが挿入され、nullではありません。

@Configuration 
@EnableGlobalMethodSecurity(jsr250Enabled=true, securedEnabled=true, prePostEnabled=true) 

私はアプリケーションを持っているコントローラに到達しようとすると、私はこの

@ModelAttribute("companies") 
    @RolesAllowed({"ROLE_ADMIN"}) 
    public Iterable<Company> companies(){ 
     return companyService.findAll(); 
    } 

ようanotatedコントローラのメソッドを持つ:@RolesAllowedを削除すると、私のapplicationConfigに

@Autowired 
CompanyService companyService; 

正常に動作します情報なしの例外:

<div th:utext="'Failed URL: ' + ${url}" th:remove="tag">${url}</div> 
<div th:utext="'Exception: ' + ${message}" th:remove="tag">${message}</div> 
<div th:utext="'Exception: ' + ${trace}" th:remove="tag">${trace}</div> 


<!-- 
    Failed URL: null 
    Exception: No message available 
    Exception: null 

    --> 

コントローラに到達する前に、私は、ユーザー

System.out.println("Authorities -> " + 
    SecurityContextHolder.getContext().getAuthentication().getAuthorities()) 

の役割を確認し、これが結果である:

Authorities -> [Authority [authority=ROLE_BASIC], Authority [authority=ROLE_ADMIN]] 

同じ結果使用:

@ModelAttribute("companies") 
    @Secured("ADMIN") 
    public Iterable<Company> companies(){ 
     return companyService.findAll(); 
    } 

又は@Secured("ROLE_ADMIN")

デバッグ中の

42410 [http-nio-8080-exec-7] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.sp[email protected]65eab2b2, returned: 1 
42410 [http-nio-8080-exec-7] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Authorization successful 
42410 [http-nio-8080-exec-7] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - RunAsManager did not change Authentication object 
42410 [http-nio-8080-exec-7] DEBUG o.s.security.web.FilterChainProxy - /company/list reached end of additional filter chain; proceeding with original chain 
42411 [http-nio-8080-exec-7] DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally 
42411 [http-nio-8080-exec-7] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed 
42411 [http-nio-8080-exec-7] DEBUG o.a.c.c.C.[Tomcat].[localhost] - Processing ErrorPage[errorCode=0, location=/error 
  • 企業()私が得たあなたが@Securedを削除する際に呼び出され、デバッグAffirmativeBasedさ:

    スイッチ(結果){ 場合AccessDecisionVoter.ACCESS_GRANTED: リターンを。 logger.debug( "Authorization successful");

+0

ログにスタックトレースはありませんか? – dur

+0

いいえ、ログにスタックトレースはありません –

+0

あなたのアプリはユーザー認証を要求していますか?セキュリティ保護されたURLにアクセスすると、ブラウザはユーザー/パスワードを要求しますか? – aaguilera

答えて

0

はこの注釈のいずれか@Securedまたは@RolesAllowed使用はもうお奨めされて使用しないでください。代わりに使用する@PreAuthorize("hasAuthority('ROLE_ADMIN')")

+0

同じエラーが発生しました:status = 404、error = Not Found :-( –

0

あなたが@Secured注釈を削除するときに呼び出さcompanies()のですか? デバッグしようとすると org.springframework.security.access.vote.AffirmativeBased が最初に呼び出されます。companies()メソッドを呼び出すときに呼び出されます。何らかの理由で2回目のチェックが失敗します。

も参照してください。 https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#authz-pre-invocation

関連する問題