【发布时间】:2018-07-19 16:17:12
【问题描述】:
我正在使用带有自定义身份验证提供程序的 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();
}
和
@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
);
}
为了测试这一点,我使用邮递员进行以下测试:
无效凭据 -> 401 未经授权
正确的凭据 -> 200 OK
无效的凭据 -> 200 OK
我的问题是最后一个请求应该返回 401 未经授权,并且成功登录后的每个后续请求都是 200 OK 即使使用错误的令牌和没有令牌。
提前致谢。
【问题讨论】:
-
您的 customauth() 方法中有什么?您可以尝试查看此示例:github.com/eugenp/tutorials/tree/master/…,如果您有省略某些内容,请尝试与您的代码进行比较。现在我唯一看到的是你没有 authenticationEntryPoint 但我不知道它是否真的需要但你可以尝试:.httpBasic() .authenticationEntryPoint(authenticationEntryPoint);
标签: java spring spring-security