简体   繁体   English

Spring Security Baisc身份验证仅验证第一个请求

[英]Spring security baisc authentication only validating first request

I'm using spring basic authentication with a custom authentication provider: 我正在使用带有自定义身份验证提供程序的spring基本身份验证:

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private CustomAuthenticationProvider authProvider;

@Override
protected void configure(
        AuthenticationManagerBuilder auth) throws Exception {

    auth.authenticationProvider(authProvider);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated()
            .and()
            .httpBasic();
}

And

    @Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String name = authentication.getName();
    String password = authentication.getCredentials().toString();

    if (customauth()) { // use the credentials
        // and authenticate against the third-party system
        {
            return new UsernamePasswordAuthenticationToken(
                    name, password, new ArrayList<>());
        }
    } else {
        return null;
    }

}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(
            UsernamePasswordAuthenticationToken.class
    );
}

To test this I'm using postman with the following tests: 为了测试这一点,我使用邮递员进行以下测试:

invalid credentials -> 401 unauthorized 无效的凭证-> 401未经授权

correct credentials -> 200 OK 正确的凭据-> 200 OK

invalid credentials -> 200 OK 无效的凭证-> 200 OK

My problem is that the last request should return 401 unauthorized and every following request after a successful login is 200 OK even with a wrong token and without token. 我的问题是最后一个请求应该返回401未经授权,并且成功登录后的每个后续请求都是200 OK,即使使用了错误的令牌也没有令牌。

Thanks in advance. 提前致谢。

When you logged in successfully, Spring Security will create an Authentication object and will put it in SecurityContext in your HTTP session. 成功登录后,Spring Security将创建一个Authentication对象,并将其放在HTTP会话的SecurityContext中。 As far as you have a valid session with a valid Authentication object at the server, Spring Security won't authenticate your request again and will use the Authentication object saved in your session. 只要服务器上有一个带有有效Authentication对象的有效会话,Spring Security便不会再次对您的请求进行身份验证,而将使用会话中保存的Authentication对象。

This is a Spring Security feature, see SEC-53 : 这是Spring Security的功能,请参阅SEC-53

Check the SecurityContextHolder for an authenticated Authentication and reuse it in that case, do not call the authentication manager again. 在SecurityContextHolder中检查经过身份验证的身份验证,并在这种情况下重新使用它,请勿再次调用身份验证管理器。

If you like to reauthenticate, you could 如果您想重新认证,可以

  • use no session at all 完全不使用会话
  • logout before reauthenticate 在重新认证之前注销

In both cases Spring Security will not find an authenticated user saved in the session and will use the new username and password for authentication. 在这两种情况下,Spring Security都不会在会话中找到经过身份验证的用户,并将使用新的用户名和密码进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Spring 安全性的仅用户名身份验证 - Username only authentication with Spring Security Spring Security-通过针对@RequestParam参数的验证原理来保护请求? - Spring security - Secure a request by validating principle against @RequestParam parameter? Spring 安全性 - 使用请求主机名进行令牌身份验证 - Spring Security - Token authentication with request host name spring security in memory authentication 首次认证后接受任意密码 - spring security in memory authentication accepts any password after first authentication crnk spring 仅安全 GET 请求工作 - crnk spring security only GET request work 即使第一个提供程序失败,Spring Security也会调用另一个身份验证提供程序 - Spring Security call another Authentication Provider even the first provider is failed Spring Security JDBC身份验证是否在每个请求上都命中数据库 - Does Spring security JDBC authentication hit the Data base on every request 具有Spring Security的双重身份验证(带有表单登录和请求) - Double authentication with spring security (with form-login and request) 我可以在使用Spring Security的身份验证时获取请求参数吗? - Can I get the request parameter at authentication time with spring security? 为一个请求禁用基本身份验证(Spring Security)并为所有任何请求禁用 - Disable Basic Authentication(Spring Security) for one request and leave for all any
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM