简体   繁体   English

为什么@AuthenticationPrincipal返回Authentication而不是主体对象?

[英]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.

相关问题 为什么 Subject.doAs 基于登录用户而不是切换主题返回主体? - Why does Subject.doAs return the principal based on the logged in user instead of the switched subject? 为什么我的Javascript返回[object HTMLScriptElement]而不是预期的文本? - Why does my Javascript return [object HTMLScriptElement] instead of expected text? @AuthenticationPrincipal返回空用户 - @AuthenticationPrincipal return empty User Spring Security HeaderPreAuthentication Principal未从@AuthenticationPrincipal解析 - Spring Security HeaderPreAuthentication Principal Not Resolved from @AuthenticationPrincipal 为什么主体在Spring Security中返回为Object类型? - Why does principal returns as type Object in spring security? 为什么在装饰器中调用安全认证属性`principal.displayName`会抛出异常? - Why does calling the security authentication property `principal.displayName` in a decorator throw an exception? AuthenticationPrincipal 返回空的 UserDetails object - AuthenticationPrincipal returns empty UserDetails object 如果身份验证对象为null,hasPermission是否返回false - Does hasPermission return false if the authentication object is null 为什么Transformer返回&lt;和&gt;而不是&lt;和&gt;? - Why does Transformer return &lt and &gt instead of < and >? 为什么此代码返回false而不是true? - Why does this code return false instead of true?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM