我正在使用 spring security (4.0.0.M2) 来保护我的 web 应用程序以及我的 rest api。一切都很好。但我有一个问题。当用户无法通过身份验证时,我想返回 401 的 http 结果 403(禁止)
我为每个身份验证场景定义了不同的 http 定义,一个用于 web,一个用于 api。
<http pattern="/rest/v?/**" auto-config="false" use-expressions="true"
disable-url-rewriting="true" authentication-manager-ref="tokenAuthenticationManager"
create-session="stateless" realm="API security check"
entry-point-ref="http403EntryPoint">
<intercept-url pattern="/rest/v?/*" access="isAuthenticated()" />
<anonymous enabled="false" />
<http-basic />
</http>
<authentication-manager id="tokenAuthenticationManager" erase-credentials="true">
<authentication-provider user-service-ref="tokenUserDetailsService" />
</authentication-manager>
public class TokenUserDetailsService implements UserDetailsService {
@Override
public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String checkUser)
throws UsernameNotFoundException {
// lookup user
if (user == null) {
// here I whant to return 403 instead of 401
throw new UsernameNotFoundException("User not found");
}
}
}
在这种情况下,有人可以帮助返回 http 状态代码 403 吗?
谢谢你的帮助。
此致
桑尼