我正在使用带有Spring security和前端reactJS的Spring boot构建一个应用程序。我的代码可以很好地进行身份验证。但现在我计划将用户重定向到他之前请求的页面,以防他必须再次登录。
我可以从成功处理程序中提取 targetUrl 即上一页,但是当我在 UI 上执行 console.log(data) 时。我得到原始 html 数据而不是 URL 名称。我不知道为什么以及如何打开这样的原始 html 代码,或者我可以只从 successhandler 发送 html URL 名称,而不是发送完整的原始 html 代码吗?任何意见都将受到高度赞赏。
预期的console.log(数据) - https://localhost:8080/addpage.html
实际console.log(data) - addpage.html的原始html数据
ApplicationSecurity.java
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/CSS/*").permitAll()
.antMatchers("/JS/*").permitAll()
.antMatchers("/login.html").permitAll()
.antMatchers("/*").authenticated()
.and()
.sessionManagement().maximumSessions(5).and().invalidSessionUrl("/login.html");
http.csrf().disable();
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
http.formLogin().successHandler(authenticationSuccessHandler);
http.formLogin().failureHandler(authenticationFailureHandler);
http.logout().logoutSuccessUrl("/login.html");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}
RESTAuthenticationSuccessHandler.java
@Component
public class RESTAuthenticationSuccessHandler extends
SavedRequestAwareAuthenticationSuccessHandler {
private RequestCache requestCache = new HttpSessionRequestCache();
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
SavedRequest savedRequest = requestCache.getRequest(request, response);
System.out.println(savedRequest);
if (savedRequest == null) {
HttpSession session = request.getSession();
if (session != null) {
String redirectUrl = (String) session.getAttribute("url_prior_login");
if (redirectUrl != null) {
session.removeAttribute("url_prior_login");
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
} else {
super.onAuthenticationSuccess(request, response, authentication);
}
} else {
super.onAuthenticationSuccess(request, response, authentication);
}
return;
}
String targetUrlParameter = getTargetUrlParameter();
System.out.println(targetUrlParameter);
if (isAlwaysUseDefaultTargetUrl()
|| (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
requestCache.removeRequest(request, response);
super.onAuthenticationSuccess(request, response, authentication);
return;
}
clearAuthenticationAttributes(request);
// Use the DefaultSavedRequest URL
String targetUrl = savedRequest.getRedirectUrl();
System.out.println(targetUrl);
logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
}
用户界面代码:
$.ajax({
type: "POST",
url: "/login",
data: data,
success: function(data){
console.log(data);
}.bind(this),
error:function(data)
{
alert(data.responseJSON.message);
}
});
console.log(data) - 返回 targetURL 的原始 html 代码,我无法将其作为 html 文件打开。我需要 targetURL 从 successhandler 返回的 html 文件名,以便我可以使用 window.location.href 打开文件
最佳答案
您可以将重定向网址
返回到响应。
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
// ... get target url
String targetUrl = "";
try {
response.setStatus(HttpServletResponse.OK);
PrintWriter writer = response.getWriter();
writer.write(targetUrl);
writer.flush();
writer.close();
} catch (IOException e) {
// ignore
logger.error(e.getMessage(), e);
}
}
关于java - 登录成功后重定向到原始页面返回原始数据而不是 URL 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43924104/