gpt4 book ai didi

java - getPrincipal() 方法返回匿名用户

转载 作者:行者123 更新时间:2023-11-30 08:44:26 24 4
gpt4 key购买 nike

我正在尝试通过 spring security 方法获取连接的用户

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

当我使用 postman 从 web 服务 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')"/>对于上述配置,它返回匿名用户。

这是我向 postman 请求的 url。

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

最佳答案

您的 intercept-url 都不匹配,因为您的路径以 services 而不是 app 开头。

如果你的上下文根是myApp,你的配置应该是:

<intercept-url pattern="/services/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/> 

关于java - getPrincipal() 方法返回匿名用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33776569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com