spring - 自定义身份验证提供程序获取每个请求的调用

标签 spring spring-mvc spring-security

我正在创建一个使用第三方系统对用户进行身份验证的自定义身份验证提供程序。用户名和密码以 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"/>

最佳答案

没关系,明白了,问题是我没有设置任何角色,所以它显示身份验证为假。在 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;
}

关于spring - 自定义身份验证提供程序获取每个请求的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653883/

相关文章:

spring - 创建名称为 'org.springframework.security.filterChains' 的 bean 时出错

user-interface - STS(Spring 工具套件)2.7.2 @RequestMappings View 不显示

session - 当浏览器关闭时 Spring Security 不会终止 session

java - 没有名为 'entityManagerFactory' 的可用 bean,但已设置entityManagerFactoryRef

java - JSON Spring 框架 Android

java - 处理 Controller 方法签名处的异常

spring - GetMapping 和 PostMapping 注释

java - Spring Maven - 应用程序未启动

java - Spring Security 无效登录处理

java - 多次限制使用公共(public)服务