[英]Why does @AuthenticationPrincipal return the Authentication instead of the principal object?
I want to retrieve the current user in my controller methods with the @AuthenticationPrincipal
annotation. 我想在我的控制器方法中使用@AuthenticationPrincipal
批注检索当前用户。 The docs state the following: 该文档指出以下内容:
Annotation that binds a method parameter or method return value to the Authentication.getPrincipal(). 将方法参数或方法返回值绑定到Authentication.getPrincipal()的注释。
But in fact I get the Authentication
object instead of Authentication.getPrincipal()
. 但是实际上我得到了Authentication
对象而不是Authentication.getPrincipal()
。
This is my simple controller method: 这是我的简单控制器方法:
@RequestMapping("/")
public @ResponseBody String index(@AuthenticationPrincipal final WindowsAuthenticationToken user) {
return String.format("Welcome to the home page, %s!", user.getName());
}
WindowsAuthenticationToken
implements Authentication
. WindowsAuthenticationToken
实现Authentication
。 In this implementation getPrincipal
returns a WindowsPrincipal
. 在此实现中, getPrincipal
返回WindowsPrincipal
。
The controller method above works, but when I change the arguments type to WindowsPrincipal
and try to access the website, I get the following error page: 上面的控制器方法有效,但是当我将参数类型更改为WindowsPrincipal
并尝试访问网站时,出现以下错误页面:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 03 15:13:52 CET 2015
There was an unexpected error (type=Internal Server Error, status=500).
argument type mismatch HandlerMethod details: Controller [pkg.HomeController] Method [public java.lang.String pkg.HomeController.index(waffle.servlet.WindowsPrincipal)] Resolved arguments: [0] [type=waffle.spring.WindowsAuthenticationToken] [value=waffle.spring.WindowsAuthenticationToken@121a2581]
This is my configuration file: 这是我的配置文件:
package pkg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import waffle.servlet.spi.BasicSecurityFilterProvider;
import waffle.servlet.spi.NegotiateSecurityFilterProvider;
import waffle.servlet.spi.SecurityFilterProvider;
import waffle.servlet.spi.SecurityFilterProviderCollection;
import waffle.spring.NegotiateSecurityFilter;
import waffle.spring.NegotiateSecurityFilterEntryPoint;
import waffle.windows.auth.impl.WindowsAuthProviderImpl;
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private NegotiateSecurityFilterEntryPoint negotiateSecurityFilterEntryPoint;
@Autowired
private NegotiateSecurityFilter waffleNegotiateSecurityFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling().authenticationEntryPoint(negotiateSecurityFilterEntryPoint).and()
.addFilterBefore(waffleNegotiateSecurityFilter, BasicAuthenticationFilter.class).authorizeRequests()
.anyRequest().fullyAuthenticated();
}
@Bean
public WindowsAuthProviderImpl waffleAuthProvider() {
return new WindowsAuthProviderImpl();
}
@Bean
public NegotiateSecurityFilterProvider negotiateSecurityFilterProvider(
final WindowsAuthProviderImpl waffleAuthProvider) {
return new NegotiateSecurityFilterProvider(waffleAuthProvider);
}
@Bean
public BasicSecurityFilterProvider basicSecurityFilterProvider(final WindowsAuthProviderImpl waffleAuthProvider) {
return new BasicSecurityFilterProvider(waffleAuthProvider);
}
@Bean
public SecurityFilterProviderCollection waffleSecurityFilterProviderCollection(
final NegotiateSecurityFilterProvider negotiateSecurityFilterProvider,
final BasicSecurityFilterProvider basicSecurityFilterProvider) {
final SecurityFilterProvider[] providers = { negotiateSecurityFilterProvider, basicSecurityFilterProvider };
return new SecurityFilterProviderCollection(providers);
}
@Bean
public NegotiateSecurityFilterEntryPoint negotiateSecurityFilterEntryPoint(
final SecurityFilterProviderCollection waffleSecurityFilterProviderCollection) {
final NegotiateSecurityFilterEntryPoint entryPoint = new NegotiateSecurityFilterEntryPoint();
entryPoint.setProvider(waffleSecurityFilterProviderCollection);
return entryPoint;
}
@Bean
public NegotiateSecurityFilter waffleNegotiateSecurityFilter(
final SecurityFilterProviderCollection waffleSecurityFilterProviderCollection) {
final NegotiateSecurityFilter filter = new NegotiateSecurityFilter();
filter.setProvider(waffleSecurityFilterProviderCollection);
return filter;
}
}
Why is the behaviour different from how it should be? 为什么行为与应有的行为有所不同?
My principal object did not implement UserDetails
. 我的主要对象没有实现UserDetails
。 Because WindowsPrincipal
is a class of an external library I could not make any changes to it. 因为WindowsPrincipal
是外部库的类,所以我无法对其进行任何更改。 In the end I created a new filter that wraps the WindowsPrincipal
in a class that implements UserDetails
. 最后,我创建了一个新的筛选器,该筛选器将WindowsPrincipal
包装在实现UserDetails
的类中。 Now I get the correct principal object using @AuthenticationPrincipal
. 现在,我使用@AuthenticationPrincipal
获得正确的主体对象。
It is because your WindowsPrincipal
implements Principal
. 这是因为WindowsPrincipal
实现Principal
。 Remove the implements clause and it will work again. 删除Implements子句,它将再次起作用。 I had the same problem and this resolved it. 我有同样的问题,这解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.