Rob*_*ans 3 java redirect spring spring-security
问题场景
我目前正在处理我的应用程序的登录页面,该页面基于Spring启动和相关的Spring项目(如安全性和云).我希望我的应用程序的用户为登录页面添加书签,因此需要采取这种行为.当我开始考虑潜在的问题时,我认为在您为页面添加书签后,应用程序将无法知道重定向到哪里(因为这可能是多个URL)(因为只有/ login且没有重定向,所以).通常,用户不会,例如,/ dashboard被重定向到登录,因为没有身份验证.在使用呈现他或她的凭证之后,应用程序重定向用户.但这只是可能的原因,应用程序在当前会话中保存SavedRequest,告知重定向位置.
我想要实现的目标
基本上我想要实现的是应用程序在用户在/ login url上设置书签后知道去哪里.这里理想的情况是/ login url包含重定向参数.例如.
现在,如果用户将对在步骤2中提供的URL添加书签,则在一段时间之后单击书签时,将向应用程序提供足够的信息以进行合理的重定向.
如果有人知道更好的方法,我想听听.
迄今采取的步骤
到目前为止,我的搜索基于StackOverflow上的另一个答案.这似乎是朝着正确方向迈出的一步,但仍然缺少一些所需的功能.
我首先创建了LoginUrlAuthenticationEntryPoint类的Custom实现.它会覆盖begin方法,如下所示:
public class CustomLoginUrlAuthenticaitonEntryPoint extends LoginUrlAuthenticationEntryPoint
{
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException
{
if (!request.getRequestURI().equals(this.getLoginFormUrl()))
{
RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
redirectStrategy.sendRedirect(request, response, getLoginFormUrl() + "?redirect=" + request.getRequestURI() + "?" + request.getQueryString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将此自定义类添加到HttpSecurity作为默认身份验证入口点.
@Configuration
@Order(-20)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.exceptionHandling()
.authenticationEntryPoint(new CustomLoginUrlAuthenticationEntryPoint("/login"));
}
}
Run Code Online (Sandbox Code Playgroud)
最后我实现了一个自定义登录控制器来为登录页面提供服务.
@Controller
public class LoginController
{
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "redirect", required = false) String redirect)
{
ModelAndView model = new ModelAndView();
// Do something with the redirect url;
model.setViewName("login");
return model;
}
Run Code Online (Sandbox Code Playgroud)
但是一旦我实现了这一点,似乎重定向工作正常.(/ dashboard?param = value被重定向到/ login?redirect =/dashboard?param = value)但是登录页面没有显示.但是当直接访问/ login url时,登录页面会显示.
所以我认为我是在/ login url中添加自定义查询参数的正确位置,但我认为实现并不完全.有人可以帮我解决问题,或者可能为我的问题提供更好的解决方案吗?
提前致谢.
Rob*_*nch 11
警告:使用参数确定要重定向到的位置可以打开应用程序,直至打开"重定向漏洞".根据用户输入执行重定向时要非常小心.
ContinueEntryPoint
您的第一步是创建一个AuthenticationEntryPoint负责包含一个带URL的参数,以便在显示登录表单时继续在URL中.在这个例子中,我们将使用参数名称continue.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.util.UrlUtils;
import org.springframework.web.util.UriComponentsBuilder;
/**
* @author Rob Winch
*
*/
public class ContinueEntryPoint extends LoginUrlAuthenticationEntryPoint {
public ContinueEntryPoint(String loginFormUrl) {
super(loginFormUrl);
}
@Override
protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) {
String continueParamValue = UrlUtils.buildRequestUrl(request);
String redirect = super.determineUrlToUseForThisRequest(request, response, exception);
return UriComponentsBuilder.fromPath(redirect).queryParam("continue", continueParamValue).toUriString();
}
}
Run Code Online (Sandbox Code Playgroud)
WebSecurityConfig
下一步是包含使用ContinueEntryPoint的安全配置.例如:
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(new ContinueEntryPoint("/login"))
.and()
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
Run Code Online (Sandbox Code Playgroud)
的LoginController
最后,如果用户已经过身份验证,您应该创建一个重定向到参数的LoginController.例如:
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.NotBlank;
public class RedirectModel {
@Pattern(regexp="^/([^/].*)?$")
@NotBlank
private String continueUrl;
public void setContinue(String continueUrl) {
this.continueUrl = continueUrl;
}
public String getContinue() {
return continueUrl;
}
}
@Controller
public class LoginController {
@RequestMapping("/login")
public String login(Principal principal, @Valid @ModelAttribute RedirectModel model, BindingResult result) {
if (!result.hasErrors() && principal != null) {
// do not redirect for absolute URLs (i.e. https://evil.com)
// do not redirect if we are not authenticated
return "redirect:" + model.getContinue();
}
return "login";
}
}
Run Code Online (Sandbox Code Playgroud)
完整样本
您可以在so-34087954-continue-on-login分支的rwinch/spring-security-sample中找到github中的完整示例.如果您不想使用git,可以轻松下载它.
| 归档时间: |
|
| 查看次数: |
8365 次 |
| 最近记录: |