在春天,我想將我的自定義身份驗證提供程序中創建的對象傳遞給我的控制器。我怎樣才能做到這一點?如何將自定義對象從自定義身份驗證提供程序傳遞到控制器?
@Component
public class CustomAuthProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String email = authentication.getName();
String password = authentication.getCredentials().toString();
// check authentication here....
// create custom object
Object customObject = ...
return new UsernamePasswordAuthenticationToken(email,password, customObject);
}
在我的控制,我想利用這個自定義對象:
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String test(Object customObject) {
//use customObject here
}
我累延長UsernamePasswordAuthenticationToken這種方式,創建自定義令牌對象:
public class CustomAuthToken extends
UsernamePasswordAuthenticationToken {
//object of any class
private Object customObject;
public CustomAuthToken(Object principal, Object credentials) {
super(principal, credentials);
this.customObject = null;
}
public CustomAuthToken(Object principal, Object credentials, Object customObject) {
super(principal, credentials);
this.customObject = customObject;
}
當我在我的自定義身份驗證提供程序中返回此令牌,出現以下錯誤:
No AuthenticationProvider found for com.example.demo.security.CustomAuthToken
這是實現我想要的正確方法嗎?我該如何解決這個錯誤?