【发布时间】:2017-12-10 14:10:38
【问题描述】:
如何在自定义身份验证提供程序中获取控制器类的调用方法名称?
REST 请求正在调用控制器方法,此请求在自定义身份验证提供程序中进行身份验证。如何在提供者中获取控制器类的方法名?
这是我的控制器方法:
@RequestMapping(value = "/customer", method = RequestMethod.GET)
@ResponseBody
public String getAllCustomers() {
return "Get customers...";
}
这是我的自定义身份验证提供程序,我想在这里获取被调用控制器方法的名称:
@Component
public class CustomAuthProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// ...
// get controller method here... -> getAllCustomers
return UsernamePasswordToken(username, password)
}
我的身份验证提供程序由一个类配置,该类扩展了 WebSecurityConfigurerAdapter 类。
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthProvider customAuthProvider;
@Autowired
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(this.customAuthProvider);
}
自定义身份验证提供程序返回 CustomAuthToken 类的对象,该对象扩展了 UsernamePasswordAuthenticationToken。我没有打电话给authenticationManager.authenticate(new MyCustomAuthenticationToken(....));
【问题讨论】:
-
你能分享一些代码吗?
-
你在哪里打电话给你的
authenticationManager.authenticate(....)? -
AuthenticationProvider 也可以是
HandlerInterceptorAdapter吗?我知道你能够告诉控制器和方法试图用HandlerMethod调用 -
@Leffchik 这是我的自定义身份验证提供程序。当用户要对服务进行身份验证时调用此提供程序。这是由扩展 WebSecurityConfigurerAdapter 的类配置的。
-
一般来说,这里的目标是从@DarrenForsythe 提到的某个地方获取
HandlerMethod。但问题是,如果你在 servlet 过滤器的某个地方调用authenticationManager.authenticate(....),那么我认为这是不可能的,因为在调用DispatcherServlet之前你不会调用HandlerMetod(并且在所有过滤器之后调用servlet.service())
标签: java spring spring-mvc spring-boot spring-security