Java Spring启动-获取控制器方法名称
如何在自定义身份验证提供程序中获取控制器类的被调用方法名 REST请求正在调用控制器方法,此请求在自定义身份验证提供程序中进行身份验证。如何获取提供程序中控制器类的方法名 以下是我的控制器方法:Java Spring启动-获取控制器方法名称,java,spring,spring-mvc,spring-boot,spring-security,Java,Spring,Spring Mvc,Spring Boot,Spring Security,如何在自定义身份验证提供程序中获取控制器类的被调用方法名 REST请求正在调用控制器方法,此请求在自定义身份验证提供程序中进行身份验证。如何获取提供程序中控制器类的方法名 以下是我的控制器方法: @RequestMapping(value = "/customer", method = RequestMethod.GET) @ResponseBody public String getAllCustomers() { return "Get customers..."; } 这是我的自
@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)
}
我的身份验证提供程序由一个类配置,该类扩展了WebSecurityConfigureAdapter类
@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(新的MyCustomAuthenticationToken(..)代码>扩展RequestMappingHandlerMapping
并跟踪方法
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
return info;
}
protected RequestMappingInfo GetMappingFormMethod(方法方法,类handlerType){
RequestMappingInfo=super.getMappingFormMethod(方法,handlerType);
退货信息;
}
RequestMappingInfo保留方法的路径。只需将信息存储在地图中,供提供商使用即可。RequestMappingHandlerMapping可以自动连接
请参见您可以共享一些代码吗?您在哪里调用您的authenticationManager。Authentication(..)
来自?AuthenticationProvider也可以是HandlerInterceptorAdapter
?我知道您可以告诉试图使用HandlerMethod
@Leffchik调用的控制器和方法这是我的自定义身份验证提供程序。当用户需要通过服务进行身份验证时,将调用此提供程序。这是由一个扩展WebSecurityConfigureAdapter的类配置的。一般来说,这里的目标是从@DarrenForsythe提到的某个地方获取HandlerMethod
。但是问题是,如果您在servlet过滤器中的某个地方调用authenticationManager.authenticate(..)
,那么我认为这是不可能的,因为在调用DispatcherServlet
之前,您不会有HandlerMetod
(并且在所有过滤器之后调用servlet.service()
)