У меня есть CustomLoginSucessHandler
в моем проекте MVC 4 Spring для управления действием, когда пользователь входит в систему.Метод defineTargetUrl весной Безопасность 4 не называется
Это нормально работает. В том же классе у меня есть метод determineTargetUrl
, чтобы перенаправить пользователя в соответствии с его ROLE
.
Вот код:
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response){
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final String userName = authentication.getName();
log.debug("TARGET URL METHOD!");
List<Authority> authorityList = authorityService.getAllAuthoritiesByUserName(userName);
for(Authority authority: authorityList){
switch (authority.getAuthority()){
case "ROLE_ADMIN":
return "processFile";
case "ROLE_USER":
return "userPortal";
case "ROLE_DEMO1":
return "processFile";
case "ROLE_DEMO2":
return "processFile";
}
}
return "403";
}
Смотрите, что у меня есть log.debug("TARGET URL METHOD")
Этот журнал никогда не вызывается и, конечно, страница не перенаправляется, это будет целевой страницы по умолчанию, processFile.html
.
Я озадачен, почему второй метод не вызывается, пока мой onAuthenticationSuccess
работает отлично. Они находятся в одном классе.
Вот код, как создать экземпляр моей CustomLoginSucessHandler
:
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
private CustomLoginSucessHandler customLoginSucessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginPage("/login.html")
.loginProcessingUrl("/login").permitAll().and().logout().logoutSuccessUrl("/")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll().and().exceptionHandling()
.accessDeniedPage("/403.html");
http.csrf().requireCsrfProtectionMatcher(new CsrfRequestMatcher());
http.formLogin().successHandler(customLoginSucessHandler);
}
Спасибо.