我们有Spring MVC应用程序.我们正在尝试将Spring安全性集成到其中.
我们编写了自定义身份验证提供程序,它将执行身份验证工作.
以下是我的自定义身份验证提供程序的代码.
public class CustomAuthenticationProvider extends DaoAuthenticationProvider {
@Autowired
private AuthenticationService authenticationService;
@Override
public Authentication authenticate(Authentication authentication) {
CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;
String username = String.valueOf(auth.getPrincipal());
String password = String.valueOf(auth.getCredentials());
try {
Users user = new User();
user.setUsername(username);
user.setPassword(PasswordUtil.encrypt(password));
user = authenticationService.validateLogin(user);
return auth;
} catch (Exception e) {
throw new BadCredentialsException("Username/Password does not match for " + username);
}
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (CustomAuthenticationToken.class.isAssignableFrom(authentication));
}
}
Run Code Online (Sandbox Code Playgroud)
这里我在以下行获取NullpointerException
user = authenticationService.validateLogin(user); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,允许经过身份验证的用户创建OAuth2承载令牌,以便与组织发布的API一起使用.这个想法是允许用户自己生成/撤销这样的令牌,类似于GitHub的Personal API令牌.然后,用户可以使用发布的令牌获得对受保护资源的编程访问.在此配置中,OAuth"客户端","授权服务器"和"资源服务器"属于组织.目前,所有这些服务都驻留在同一个流程中.
为此,我正在尝试支持资源所有者密码凭据授予类型.实施环境包括以下内容:
实现的一个约束是无法访问存储的密码.此处理委托给执行实际身份验证的内部Web服务.
由于无法访问存储密码的限制,因此无法使用默认配置,DaoAuthenticationProvider因为它需要访问UserDetails由提供程序返回的对象提供的密码UserDetailsService.
我的猜测是我需要AuthenticationProvider用自定义实现替换它.但是,所有这样做的尝试似乎都没有生效.
以下似乎在parent引用中正确注册AuthenticationManager,但未在运行时委托(由于DaoAuthenticationProvider优先权):
@Configuration
public class SecurityConfig extends GlobalAuthenticationConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthenticationProvider() {
@Override
public boolean supports(Class<?> authentication) {
// For testing this is easier, but should check for UsernamePasswordAuthentication.class
return true;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// …Run Code Online (Sandbox Code Playgroud) spring-security basic-authentication spring-boot spring-security-oauth2