成功登录后重定向到原始页面将返回原始数据而不是URL名称 [英] Redirecting to original page after successful login returns raw data instead of URL name
问题描述
我正在使用具有Spring安全性和前端reactJS的Spring Boot构建应用程序.我的代码可以很好地与身份验证一起使用.但是现在我打算将用户重定向到他先前请求的页面,以防他不得不再次登录.
I am building an application using Spring boot with Spring security and front end reactJS. My code works well with authentication. But now i am planning to redirect the user to his previous requested page in case he has to login again.
我可以从成功处理程序中提取targetUrl(即上一页),但是当我在UI上执行console.log(data)时.我得到原始的html数据而不是URL名称.我不知道为什么以及如何打开这样的原始html代码,或者我可以仅从Successhandler发送html URL名称,而是发送完整的原始html代码吗?任何输入都将受到高度赞赏.
I can extract targetUrl i.e. previous page from the successhandler but when i do a console.log(data) at the UI. I get the raw html data instead of URL name. I dont know why and how to open such a raw html code or can i send just html URL name from successhandler instead it sends complete raw html code? Any inputs are highly appreciated.
预期的console.log(数据)- https://localhost:8080/addpage.html
Expected console.log(data) - https://localhost:8080/addpage.html
Actual console.log(data)-addpage.html的原始html数据
Actual console.log(data) - raw html data of addpage.html
ApplicationSecurity.java
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
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);
}
}
UI代码:
$.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)-返回无法作为html文件打开的targetURL的原始html代码.我需要由targetURL从successhandler返回的html文件名,以便可以使用window.location.href打开文件
console.log(data) - returns raw html code of targetURL which i cannot open as a html file. I need the html file name returned by targetURL from successhandler so that i can use window.location.href to open the file
推荐答案
您可以返回redirect url
进行响应.
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);
}
}
这篇关于成功登录后重定向到原始页面将返回原始数据而不是URL名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!