2017-08-25 15 views
9

私はSpring WebアプリケーションでSpring Securityを統合しようとしています。基本的に私はユーザーの許可に基づいていくつかのメニューを非表示にする必要があります。ここに私がしたことがあります。春のセキュリティhasPermissionが動作していません

クラスパスのJARS以下に追加しました。以下は

spring-security-acl-4.0.2.RELEASE.jar 
spring-security-config-4.0.2.RELEASE.jar 
spring-security-core-4.0.2.RELEASE.jar 
spring-security-taglibs-4.0.1.RELEASE.jar 
spring-security-web-4.0.2.RELEASE.jar 

私は以下のようなクラスCustomPermissionEvaluatorを書いたweb.xmlのエントリ

<context-param> 
    <param-name>log4jConfiguration</param-name> 
    <param-value>/WEB-INF/web_log4j.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-root.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

です。

public class CustomPermissionEvaluator implements PermissionEvaluator{ 


@Override 
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) { 
    HttpServletRequest request = (HttpServletRequest) targetDomainObject; 
    Profile userProfile = (Profile) request.getSession().getAttribute("testprofile"); 
    if (userProfile.getPermissionMap().get(String.valueOf(permission)) != null) { 
     return true; 
    } else { 
     return false; 
    } 
} 

@Override 
public boolean hasPermission(Authentication arg0, Serializable arg1, 
     String arg2, Object arg3) { 
    // TODO Auto-generated method stub 
    return false; 
} 

}

はこの後、私はSecurityConfigファイルを書きました。今私のJSPファイル内のtaglib私は下に使用しています

<sec:global-method-security pre-post-annotations="enabled"> 
    <sec:expression-handler ref="expressionHandler" /> 
</sec:global-method-security> 
<bean id="expressionHandler" 
    class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler"> 
    <property name="permissionEvaluator" ref="permissionEvaluator" /> 
</bean> 
<bean id="permissionEvaluator" class="main.java.com.config.CustomPermissionEvaluator" /> 

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

@Override 
public void configure(WebSecurity web) throws Exception { 
    DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler(); 
    handler.setPermissionEvaluator(new CustomPermissionEvaluator()); 
    web.expressionHandler(handler); 
} 

}

私は私の春-root.xmlのエントリの下に持っています。

とコード

<sec:authorize access="hasPermission('cadastra_categoria', #request)">  
       <div id="TEST"> 
       </div> 
      </sec:authorize> 

以下

しかし、それは動作しません。どんなsuggesationが評価されます。

+0

インジケータ、エラーか何かがあります:

は私が直接、任意のエラーがあるかもしれない明確な私のポイントのコードCustomPermissionEvaluatorを書いていますか?ブレークポイントを配置するとき、hasPermissionコールが実装に達しましたか?私はあなたがセキュリティのフィルターチェーンhttps://docs.spring.io/spring-security/site/docs/4.2.3が不足していると思います。RELEASE/reference/htmlsingle /#ns-web-xmlですが、その前提は – David

+0

です。いいえ、どちらもエラーが発生せず、CustomPermissionEvaluatorへの呼び出しもありません。 –

+0

Beanを正しく参照していますか? 'class =" main.java.com.config.CustomPermissionEvaluator "' - これは 'main.java'なしではいけませんか? – aturkovic

答えて

0

hasAnyRoleを試してみて、一度すなわち

<sec:authorize access="hasAnyRole('ROLE_NAME')"> TEST </sec:authorize> 
+0

hasAnyRoleではなくhasPermissionが必要です。 –

2

"hasPermission('cadastra_categoria', #request)"

を確認してください実際には、有効なコールは最初のものは、対象ドメインオブジェクトと第二でなければならない、スワップの引数を持たなければならない - 許可:

hasPermission(#request, 'cadastra_categoria') 

また、必要に応じてsec taglibをJSPにインポートしたことを再度確認したとします。

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> 

そしてこのanswerの2番目の部分で明らかなように、最終的に定義以下:私はあなたが持っているあなたの質問を理解したよう

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

public class AnnotationConfigDispatcherServletInitializer extends 
    AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
    return new Class[] { 
     SecurityConfig.class //your SecurityConfig 
    }; 
    } 
} 

Webアプリケーションの起動

+0

私は昨日私の問題を解決した人たちに任せて、解決策でアップデートします。手伝ってくれてありがとう。 –

2

中に呼び出されることを確認configure(WebSecurity web)を作るためにCustomPermissionEvaluatorクラスを作成しましたが、認証されたユーザーのアクセス許可でチェックしていません。

public class CustomPermissionEvaluator implements PermissionEvaluator { 

    public boolean hasPermission(Authentication auth, Object targetDomainObject, Object permission) { 
     if ((auth == null) || (targetDomainObject == null) || !(permission instanceof String)){ 
      return false; 
     } 

     Profile userProfile = (Profile) request.getSession().getAttribute("testprofile"); 
     String targetType = userProfile.getPermissionMap().get(String.valueOf(permission)); 

     return hasPrivilege(auth, targetType, permission.toString().toUpperCase()); 
    } 

    private boolean hasPrivilege(Authentication auth, String targetType, String permission) { 
     for (GrantedAuthority grantedAuth : auth.getAuthorities()) { 
      if (grantedAuth.getAuthority().startsWith(targetType)) { 
       if (grantedAuth.getAuthority().contains(permission)) { 
        return true; 
       } 
      } 
     } 
     return false; 
    } 

    @Override 
    public boolean hasPermission(Authentication arg0, Serializable arg1, String arg2, Object arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 
関連する問題