2017-03-08 10 views
1

http://forum.spring.io/forum/spring-projects/security/100708-spel-and-spring-security-3-accessing-bean-reference-in-preauthorize の例のように、Spring Security内で@PreAuthorizeをSpELで使用したいと思っていますが、これはSpring Security 4.1.4で使用している間は機能しません。以下は私のサンプルコードです:accessBean.getPermision()」式を評価するために失敗しましたSpringセキュリティで他のBeanとメソッドを使用する@PreAuthorize

Beanクラス:コントローラで

package com.service.spel; 

import org.springframework.stereotype.Component; 

@Component(value="accessBean") 
public class AccessCheckBean { 
    public String getPermision(){ 
     /** 
     * return the api permision 
     */ 
     String scope = "#oauth2.hasScope('resource.Update')"; 
     return scope; 
    } 
} 

@PreAuthorize("accessBean.getPermision()") 
    @GetMapping("/perm") 
    public @ResponseBody String getPerm(){ 
     return "Perm"; 
    } 

エラーメッセージ'

私は上記のようなSpELを使用できないようですが、このバージョンのSpring Securityを使用している場合、どのように進めることができますか?

答えて

1

あなたはSpring Security Referenceを見、@を使用する必要があります。

あなたが利用可能な表現を拡張したい場合、あなたは簡単にあなたが公開するすべての春の豆を参照することができます。たとえば、次のメソッドのシグネチャが含まれているwebSecurityの名前で豆を持っていると仮定すると:

<http> 
    <intercept-url pattern="/user/**" 
     access="@webSecurity.check(authentication,request)"/> 
    ... 
</http> 

またはJava構成

http 
     .authorizeRequests() 
       .antMatchers("/user/**").access("@webSecurity.check(authentication,request)") 
       ... 
中を:

public class WebSecurity { 
     public boolean check(Authentication authentication, HttpServletRequest request) { 
       ... 
     } 
} 

あなたは使用する方法を参照してください可能性があり

関連する問題