簡體   English   中英

Spring Boot-獲取控制器方法名稱

[英]Spring Boot - Get controller method name

如何在自定義身份驗證提供程序中獲取控制器類的被調用方法名稱?

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(....));

擴展RequestMappingHandlerMapping並跟蹤方法

protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
    RequestMappingInfo info = super.getMappingForMethod(method, handlerType);

    return info;
}

RequestMappingInfo保留方法的路徑。 只需將信息存儲在地圖中即可在您的提供商中使用。 RequestMappingHandlerMapping可以自動裝配。

請參閱如何使用@RequestMapping在Spring MVC Controller中優化我的代碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM