Spring如何在登录URL上设置动态前缀

Hyp*_*nos 5 java spring login spring-mvc

我有一个总是以动态前缀开头的spring应用程序。这是因为我需要该前缀来进行一些内部配置。问题是,当我尝试设置登录页面时,无法传递该前缀并开始工作。如何在登录页面中设置动态前缀?

这是我AppController的一部分,我在其中分配请求映射以使用我的前缀。

AppCotroler.java

@Controller
@RequestMapping("/{myDynamicPrefix}")
public class AppController {

    ...... @PathVariable String myDynamicPrefix // To ascces to the variable
}
Run Code Online (Sandbox Code Playgroud)

这是我的WebSecurityConfigurerAdapter的一部分,其中分配了登录页面。

WebSecurityConfig.java

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MyDBAuthenticationService myDBAauthenticationService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {


        http.csrf().disable();

        http.authorizeRequests().antMatchers("/{myDynamicPrefix}/login").permitAll();
        // If no login, it will redirect to /login page.
        http.authorizeRequests().antMatchers("/","/{myDynamicPrefix}/**").access("hasAnyRole('ROLE_USER')");
        http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");

        // Config for Login Form
        http.authorizeRequests().and().formLogin()//
                // Submit URL of login page.
                .loginProcessingUrl("/j_spring_security_check") // Submit URL
                .loginPage("/{myDynamicPrefix}/login")// 
                .defaultSuccessUrl("/userInfo")//
                .failureUrl("/{myDynamicPrefix}/login?error=true")//
                .usernameParameter("username")//
                .passwordParameter("password")
                // Config for Logout Page
                .and().logout().logoutUrl("/{myDynamicPrefix}/logout").logoutSuccessUrl("/{myDynamicPrefix}/logoutSuccessful");

    }
Run Code Online (Sandbox Code Playgroud)

认为是.antMatchers("/{myDynamicPrefix}/login")并且.antMatchers("/","/{myDynamicPrefix}/**")正在正常工作,但是.loginPage("/{myDynamicPrefix}/login")工作,根本没有工作。

这是我想要网址的示例

http://localhost:8080/MyApp/MyPrefix/MyPage
Run Code Online (Sandbox Code Playgroud)