Использование Spring-Boot 1.1.17, Spring MVC- с Спринг-Security:весной отображение безопасности для групповых символов
У меня есть несколько субдоменов, которые я хочу, чтобы позволить непроверенным пользователям (посетители) доступ. Например:
- mysite.com/customerA
- mysite.com/customerB
Если недействительный сайт клиент пытался, то мой контроллер будет либо выбросить исключение или перенаправление на/(mysite.com/) Естественно, что другие части домена (mysite.com/customerA/myaccount) потребуют входа в систему.
Я действительно не понял, как это сделать с весной безопасности и весной-mvc. Вот что я пытаюсь до сих пор:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(new CSRFTokenGeneratorFilter(), CsrfFilter.class)
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/**/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/wizard").permitAll()
.antMatchers("/menu").permitAll()
.antMatchers("/error").permitAll()
.antMatchers("/resources/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/fonts/**").permitAll()
.antMatchers("/libs/**").permitAll();
http
.formLogin()
.loginPage("/loginPage")
.permitAll()
.loginProcessingUrl("/login")
.failureUrl("/login?error")
.defaultSuccessUrl("/?tab=success")
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/")
.permitAll()
.and()
.csrf();
http
.sessionManagement()
.maximumSessions(1)
.expiredUrl("/login?expired")
.maxSessionsPreventsLogin(true)
.and()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.invalidSessionUrl("/");
http
.authorizeRequests().anyRequest().authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = new BCryptPasswordEncoder();
auth.userDetailsService(customUserDetailsService).passwordEncoder(encoder);
}
@Override
public void configure(WebSecurity security){
security.ignoring().antMatchers("/css/**","/fonts/**","/libs/**");
}
}
И на моей домашней странице контроллера:
@Controller
@RequestMapping("/{officeName}/")
public class HomeController {
private AuthenticatedUser getVisitor(@PathVariable String officeName) {
.. do something with the office if found, redirect otherwise
if (!StringUtils.isEmpty(officeName)) {
Office office = officeService.findByName(officeName);
return office.getUrl();
}
return "/";
}
Когда я пытаюсь получить доступ к этому URL, я получаю следующие ошибки:
o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/customerA/]
s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /customerA/
s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/customerA/]
o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/customerA/] are [/**]
o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/customerA/] are {}
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/customerA/] to HandlerExecutionChain with handler [org.[email protected]2f295527] and 1 interceptor
o.s.web.servlet.DispatcherServlet : Last-Modified value for [/customerA/] is: -1
o.s.w.s.r.ResourceHttpRequestHandler : Trying relative path [customerA] against base location: ServletContext resource [/]
o.s.w.s.r.ResourceHttpRequestHandler : Trying relative path [customerA] against base location: class path resource [META-INF/resources/]
o.s.w.s.r.ResourceHttpRequestHandler : Trying relative path [customerA] against base location: class path resource [resources/]
o.s.w.s.r.ResourceHttpRequestHandler : Trying relative path [customerA] against base location: class path resource [static/]
o.s.w.s.r.ResourceHttpRequestHandler : Trying relative path [customerA] against base location: class path resource [public/]
o.s.w.s.r.ResourceHttpRequestHandler : No matching resource found - returning 404
I попробовал добавить этот ServletRegistrationBean:
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("/", "/testCustomer/*" );
for (Office office : officeService.findAllActiveOffices()) {
registration.addUrlMappings(office.getUrl() + "/*");
}
return registration;
}
Но это, похоже, работает только в том случае, если приложение знает о клиенте при запуске, а не динамически в случае регистрации клиента.
Есть ли способ настроить это для обработки этих типов подстановочных знаков?
Это частный метод с привязкой '@ PathVariable' (вероятно, не будет работать с Spring MVC)? –
@Dave - oops, спасибо за улов. К сожалению, это действительно не повлияло на мою проблему. Подход ServletRegistrationBean, казалось бы, стал началом, есть ли в любом случае динамическое обновление ServletRegistrationBean во время выполнения (т. Е. Новый клиент подписывается)? – sonoerin
Я действительно не понимаю случай использования, если вам нужно это сделать. Spring MVC может динамически сопоставлять пути (как вы пытаетесь сделать с '@ PathVariable'), поэтому вам действительно нужно использовать эту функцию. –