這是我的配置彈簧4 Ajax登陸重定向到請求的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("");
}
它執行身份驗證並調用get方法。
@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);
}
所以當我打開/管理我重定向到/#登錄。登錄對話框打開,登錄成功,但我喜歡重定向到最初請求的URL。
原始URL應該由Spring知道,作爲AJAX登錄的響應返回,並且JavaScript應該改變位置。