Spring How to set dynamic prefix for login url

I have a spring application that always starts with a dynamic prefix. This is because I need this prefix to create some internal configurations. The problem is that when I try to set my login page, it is not possible to pass this prefix and get the job done. How to set a dummy prefix on my login page?

This is part of my AppController where I assign the Mapping Request to have my prefix.

Appcotroler.java

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

    ...... @PathVariable String myDynamicPrefix // To ascces to the variable
}

And this is part of my WebSecurityConfigurerAdapter adapter, which introduces the login page.

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");

    }

I think that .antMatchers("/{myDynamicPrefix}/login")they .antMatchers("/","/{myDynamicPrefix}/**")work fine, but .loginPage("/{myDynamicPrefix}/login")they don’t work at all.

This is an example of how I want a URL

http://localhost:8080/MyApp/MyPrefix/MyPage
+4

Source: https://habr.com/ru/post/1678256/


All Articles