2015-11-18 687 views
1

我試圖獲得通過Spring Security的方法getPrincipal()方法返回匿名用戶

public static User getConnectedUser() { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    User currentUser=auth.getPrincipal(); 
} 

連接的用戶,當我從一個Web服務調用此方法使用郵遞員返回anonymousUser get方法,即使是在記錄。 這裏是我的春天的安全配置:

<http auto-config="true" use-expressions="true"> 
    <intercept-url pattern="/app/admin/**" access="hasRole('ROLE_ADMIN')"/> 
    <intercept-url pattern="/app/passwordHint*" access="hasAnyRole('ROLE_USER','ROLE_ADMIN','ROLE_ANONYMOUS')"/> 
    <intercept-url pattern="/app/requestRecoveryToken*" access="hasAnyRole('ROLE_USER','ROLE_ADMIN','ROLE_ANONYMOUS')" /> 
    <intercept-url pattern="/app/updatePassword*" access="hasAnyRole('ROLE_USER','ROLE_ADMIN','ROLE_ANONYMOUS')" /> 
    <intercept-url pattern="/app/signup*" access="hasAnyRole('ROLE_USER','ROLE_ADMIN','ROLE_ANONYMOUS')"/> 
    <intercept-url pattern="/app/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/> 
    <form-login login-page="/login" authentication-failure-url="/login?error=true" login-processing-url="/j_security_check"/> 
    <remember-me user-service-ref="userDao" key="e37f4b31-0c45-11dd-bd0b-0800200c9a66"/> 
    <custom-filter ref="ajaxTimeoutRedirectFilter" after="EXCEPTION_TRANSLATION_FILTER"/> 
</http> 

甚至當我加入這行<intercept-url pattern="/*" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>它返回anonymousUser上述配置。

以下是我向郵遞員索要的網址。

http://localhost:8080/myApp/services/api/seal/ 

回答

2

尤爾intercept-url都無法比擬的,因爲你的路徑與servicesapp開始。

如果上下文根是myApp,你的配置應該是:

<intercept-url pattern="/services/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/> 
與此`<截距-URL圖案= 「/ *」 訪問=
+0

甚至 「hasAnyRole( 'ROLE_USER', 'ROLE_ADMIN')」 />'? –