我正在尝试通过spring安全方法获取连接用户
public static User getConnectedUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User currentUser=auth.getPrincipal();
}
当我使用postman从webservice get方法调用此方法时,即使登录,它也会返回anonymousUser。 这是我的spring安全配置:
<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/
答案 0 :(得分:2)
Yor intercept-url
都不匹配,因为您的路径以services
而不是app
开头。
如果您的上下文根为myApp
,则您的配置应为:
<intercept-url pattern="/services/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>