Spring boot and spring security multiple login pages

前端 未结 2 1418
无人共我
无人共我 2021-01-16 08:36
@EnableWebSecurity
public class MultiHttpSecurityConfig {

@Configuration
@Order(1)
public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-16 09:30

    I reckon that the reason why your admin login is not activating is because: first, it is NOT higher in priority.

    @Order defines the sort order for an annotated component. The value is optional and represents an order value as defined in the Ordered interface. Lower values have higher priority. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).

    Second, according to HttpSecurity's Javadoc:

    A HttpSecurity is similar to Spring Security's XML element in the namespace configuration. It allows configuring web based security for specific http requests. By default it will be applied to all requests, but can be restricted using requestMatcher(RequestMatcher) or other similar methods.

    So try restricting the HttpSecurity object to activate for your admin pages by first configuring the requestMatcher such that:

        http
          .requestMatcher(new AntPathRequestMatcher("/admin/**"))
          .csrf().disable()      
          .authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
          .and().formLogin().loginPage("/adminlogin");
    

提交回复
热议问题