【问题标题】:Custom Authentication Provider get calls with every request自定义身份验证提供程序获取每个请求的调用
【发布时间】:2015-02-23 13:46:51
【问题描述】:

我正在创建一个自定义身份验证提供程序,它使用第三方系统对用户进行身份验证。用户名和密码以 json 格式发送到服务器。为了实现这一点,我创建了一个自定义过滤器——在 FORM_LOGIN_FILTER 位置调用的 UsernamePasswordAuthenticationFilter。在此之后,我创建了一个自定义身份验证提供程序来使用第三方系统对用户进行身份验证。但是,每个请求都会调用此身份验证过滤器,这会导致每个请求都会调用第三方系统。我做错了什么?

自定义用户名密码验证过滤器:

@Override
public Authentication attemptAuthentication( HttpServletRequest request, HttpServletResponse response)
{
    //Get username password from request
    UsernamePasswordAuthenticationToken token = 
            new UsernamePasswordAuthenticationToken( username, password);
    setDetails(request, token);
    return this.getAuthenticationManager().authenticate(token);
}

自定义身份验证提供程序:

@Override
public Authentication authenticate(Authentication authentication) {
      String username = authentication.getName();
      String password = authentication.getCredentials().toString();
      boolean flag = //use the credentials to try to authenticate against the third party system
    if(flag) {
        return new UsernamePasswordAuthenticationToken(username, password);
    }
    else
        throw new BadCredentialsException("Bad Credentials");
}

@Override
public boolean supports(Class<?> authentication) {
    return true;
}

安全上下文.xml

<http pattern="/resources/**" security="none"/>
<http auto-config="false" use-expressions="true" access-denied-page="/welcome"
      create-session="always" disable-url-rewriting="true" entry-point-ref="customEntryPoint">
    <intercept-url pattern="/" access='permitAll'/>
    <custom-filter ref="loginFilter" position="FORM_LOGIN_FILTER" />
    <intercept-url pattern="/**" access="isAuthenticated()" />
    <logout logout-success-url="/" delete-cookies="JSESSIONID" logout-url="/logout"  invalidate-session="true" />
</http>

<bean id="loginFilter" class="org.temp.secure.CustomUsernamePasswordAuthenticationFilter">
    <beans:property name="requiresAuthenticationRequestMatcher" ref="loginRequestUrlHandler" />
    <beans:property name="authenticationManager" ref="authenticationManager" />
    <beans:property name="usernameParameter" value="username" />
    <beans:property name="passwordParameter" value="password" />
</beans:bean>

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="customAuthenticationProvider" />
</authentication-manager>

<bean id="loginRequestUrlHandler" class="org.springframework.security.web.util.matcher.RegexRequestMatcher">
    <constructor-arg index="0" value="/login" />
    <constructor-arg index="1" value="POST" />
    <constructor-arg index="2" value="false" />
</bean>

<bean id="customEntryPoint" class="org.temp.secure.CustomEntryPoint" />

<bean id="customAuthenticationProvider" class="org.temp.secure.MyAuthenticationProvider"/>

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    没关系,明白了,问题是我没有设置任何角色,所以它显示身份验证为假。在 UsernamePasswordAuthenticationToken 中设置角色后,不再调用自定义身份验证提供程序..

    @Override
    public Authentication authenticate(Authentication authentication) {
        String username = authentication.getName();
        String password = authentication.getCredentials().toString();
        boolean flag = //use the credentials to try to authenticate against the third party system
        if(flag) {
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new SimpleGrantedAuthority("ROLE_ONE"));
            authorities.add(new SimpleGrantedAuthority("ROLE_TWO"));
            return new UsernamePasswordAuthenticationToken(username, password, authorities);
       }
       else
           throw new BadCredentialsException("Bad Credentials"); 
    }
    
    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }
    

    【讨论】:

    • 你能告诉你在哪里配置了 CustomUsernamePasswordAuthenticationFilter。还需要编写 CustomUsernamePasswordAuthenticationFilter 吗?仅使用“自定义身份验证提供程序”就可以了。
    • 拯救了我的一天。我正在使用自定义Authentication 实现来实现自定义身份验证,并将authenticated 默认设置为false,以后无需更改它。每次通话都会触发我的AuthenticationProvider。成功验证后将其设置为true 解决了我的问题。
    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 2012-11-09
    • 2011-01-03
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    相关资源
    最近更新 更多