【发布时间】:2020-07-20 13:37:33
【问题描述】:
我们有登录页面,用户将在其中输入用户凭据并在内部调用一个需要存储此令牌并传递给所有 REST 控制器的身份验证服务。我尝试在此类中配置 bean 范围,但低于异常。我们正在使用春天 5.x;
com.config.CustomAuthenticationProvider sessionScopedBean CustomAuthenticationProvider 用户详细信息 !!!null 2020 年 6 月 20 日上午 11:52:37 org.apache.catalina.core.StandardWrapperValve 调用
java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean 无法转换为 com.utils.UserDetails
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private Logger logger = Logger.getLogger(getClass().getName());
private UserDetails userDetails;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userName = authentication.getName();
String passWord = authentication.getCredentials().toString();
Result response;
try {
response = CustomClient.authenticate(userName, passWord);
} catch (Exception e) {
throw new BadCredentialsException("system authentication failed");
}
if (response != null && response.getToken() != null) {
//need to store this response.getToken() in session
logger.info("Token: " + response.getToken());
userDetails= new UserDetails();
userDetails.setToken(response.getToken());
logger.info("Authentication SUCCESS !!!");
return new UsernamePasswordAuthenticationToken(userName, passWord, Collections.emptyList());
} else {
logger.info("Authentication FAILED...");
throw new BadCredentialsException("authentication failed");
}
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserDetails sessionScopedBean() {
logger.info(" UserDetails !!!"+userDetails);
return userDetails;
}
@Override
public boolean supports(Class<?> auth) {
return auth.equals(UsernamePasswordAuthenticationToken.class);
}
}
【问题讨论】:
标签: spring spring-mvc spring-security