Это часть моей конфигурацииSpring 4 Аякса Войти перенаправлять запрошенному URL
public class CustomWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
private static final String LOGIN_URL = "/#login";
@Override
protected void configure(final HttpSecurity http) throws Exception {
final String adminAccess = String.format("hasAnyRole('ROLE_%s', 'ROLE_%s')",
Role.SYSTEM_ADMINISTRATOR, Role.USER_ADMINISTRATOR);
http.authorizeRequests().antMatchers("/admin/**").access(adminAccess).and().formLogin()
.loginPage(LOGIN_URL);
}
}
Как можно видеть, только Войти запускает стартовую страницу и #login используется, чтобы показать диалог модальный входа в систему, который посылает войдите в систему с помощью AJAX.
Следующий код - это логин.
@RequestMapping(value = "/login", method = RequestMethod.POST)
ResponseEntity<Map<String, Object>> login(@RequestBody final JSONCredentials credentials) {
log.debug("Test: {}", requestCache == null ? "null" : requestCache.getClass());
final Authentication auth = new UsernamePasswordAuthenticationToken(credentials.getUsername(),
credentials.getPassword());
final Authentication authenticated = authenticationManager.authenticate(auth);
if (authenticated.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authenticated);
return get();
}
SecurityContextHolder.clearContext();
throw new BadCredentialsException("");
}
он выполняет метод аутентификации и звонков.
@RequestMapping(method = RequestMethod.GET)
ResponseEntity<Map<String, Object>> get() {
final Map<String, Object> result = new HashMap<>();
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
result.put("principal", auth.getPrincipal());
}
result.put("redirectTo", "URL where I come from");
return ResponseEntity.ok(result);
}
Поэтому, когда я открываю/admin, я перенаправлен в/# login. Откроется диалоговое окно входа в систему, и вход в систему будет успешным, но мне нравится перенаправлять исходный URL.
Исходный URL должен быть известен весной, возвращен как ответ входа в систему AJAX, а JavaScript просто должен изменить местоположение.