- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题场景
我目前正在开发基于 Spring boot 和相关 Spring 项目(如安全和云)的应用程序的登录页面。我希望我的应用程序的用户为登录页面添加书签,因此需要采取这种行为。当我开始考虑潜在的问题时,我认为应用程序将无法知道在您为页面添加书签后重定向到哪里(因为这可以是多个 url)(因为只有/login 而没有重定向)。通常用户不会,例如,/dashboard 并被重定向到登录,因为不存在身份验证。在用户出示他或她的凭据后,应用程序会重定向用户。但这是唯一可能的原因,因为应用程序在其当前 session 中持有一个 SavedRequest 告诉重定向位置。
我想要实现的目标
基本上我想要实现的是让应用程序在用户在/login url 上设置书签后知道去哪里。这里的理想情况是/login url 包含一个重定向参数。例如。
现在,如果用户将在步骤 2 中提供的 url 加入书签,那么在一段时间后单击书签后,将为应用程序提供足够的信息以进行合理的重定向。
如果有人知道更好的方法,我想听听。
到目前为止采取的步骤
到目前为止,我一直在寻找解决方案 another answer on StackOverflow .这似乎是朝着正确方向迈出的一步,但仍然缺少一些所需的功能。
我首先创建了 LoginUrlAuthenticationEntryPoint 类的自定义实现。它覆盖了开始方法,看起来像这样:
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());
}
}
}
然后我将这个自定义类添加到 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"));
}
}
最后我实现了一个自定义登录 Controller 来为登录页面提供服务。
@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;
}
但是一旦我实现了这个,重定向似乎就可以正常工作了。 (/dashboard?param=value 已重定向到/login?redirect=/dashboard?param=value)但未显示登录页面。但是当直接访问/login url 时,登录页面确实会显示。
所以我认为我是在正确的位置将自定义查询参数添加到/login url,但我猜实现还不是很完整。有人可以帮我解决问题,或者为我的问题提供更好的解决方案吗?
提前致谢。
最佳答案
警告:使用参数来确定您要重定向到的位置最多可以打开您的应用程序 Open Redirect Vulnerabilities .根据用户输入执行重定向时要非常小心。
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();
}
}
网络安全配置
下一步是包括一个使用 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();
}
}
登录 Controller
最后,如果用户已经通过身份验证,您应该创建一个重定向到参数的 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";
}
}
完整样本
您可以在 github 中的 rwinch/spring-security-sample 找到完整的示例在 so-34087954-continue-on-login 分支中。您可以轻松download如果您不想使用 git,请使用它。
关于java - Spring 安全 : How to add a redirect query parameter to the login url to allow bookmarking of the page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34087954/
目前我正在尝试创建一个 Web 部署包。所以我在我的项目的根目录中添加了一个 parameters.xml 并指定了一些自定义参数。 我发现我的很多参数都部分相同。所以我想做某种参数引用。寻找这个,我
如何设置我的 Symfony 2 项目以使用 parameters.yml 而不是 parameters.ini? 在 Controller 中,我可以像这样从 parameters.ini 中获取变
有什么建议说明为什么此 AWS CloudFormation 不断回滚吗? { "Description" : "Single Instance", "Resources" : {
PARAMETERS: p_1 TYPE i, p_2 TYPE i. 因此在初始屏幕中,我看到了 2 个文本框,每个参数一个。 如果我填写其中一个,但不按回车键,然后我在第二个上调用 F4 帮助,我
我需要存储 Parameter由 Build() 返回作为 Parameter (因为我将参数存储在一个数组中,另一种方法就是为每个参数数量复制粘贴相同的类太多,因为 c# 没有可变参数泛型)。 问题
我正在为我的 CS 类(class)做作业,它使用了 const,我对何时使用它们感到有点困惑。 这3个函数有什么区别? int const function(parameters) int fu
在 xgboost 的文档中,我读到: base_score [default=0.5] : the initial prediction score of all instances, global
我正在创建一个新的 REST 服务。 向 REST 服务传递参数的标准是什么。在 Java 的不同 REST 实现中,您可以将参数配置为路径的一部分或请求参数。例如, 路径参数 http://www.
在我的程序中,我需要验证传递给程序的参数是一个整数,所以我创建了这个小函数来处理用户键入“1st”而不是“1”的情况。 问题是它根本不起作用。我尝试调试,我只能告诉你参数是 12,long 是 2。(
谁能告诉我如何使用存储在 &rest 指定值中的参数。 我已经阅读了很多,似乎作者只知道如何列出所有参数。 (defun test (a &rest b) b) 这很高兴看到,但并不是很有用。 到目前
我使用 git 有一段时间了,但大多数时候我更喜欢与 Intelij IDEA 的集成。现在,为了扩展我对系统的知识和理解,我决定更多地使用命令行。我观察到的是有两种类型的参数: --paramete
我正在用 RAML 编写一些 REST 文档,但我被卡住了。 我的问题: - 我有一个用于搜索的 GET 请求,它可以采用参数“id”或( 独占或 )“引用”。拥有 只有其中之一 是必须的。 我知道怎
我定义了一个这样的 Action : /secure/listaAnnunci.action /login.jsp 我可以从 Action 内部访问参数吗?谢谢 最佳答案 您需要实现 S
我有一个 TeamCity 8.0.3 项目,其中包含多个配置,其中有一个通用参数(定义为项目参数):targetServerIP .这些配置之一是“一键部署”,它通过使用快照依赖项启动其他配置。我已
try{ Class.forName("com.mysql.jdbc.Driver"); mycon = DriverManager.getConnec
我在实际的 javascript 项目中遇到了一个非常奇怪的情况。我创建了一个自定义事件并将数组放入该事件 $.publish("merilon/TablesLoaded", getTables())
在使用参数数组进行插入/更新期间,可以忽略一个/一些特定行的一个/一些参数。 我提供了一个简单的例子。想象一下,我们有一个包含 3 列的表:X、Y 和 Z。我们想在 block 中执行更新(如果缺少某
如何编写接受未定义参数的函数?我想它可以像那样工作: void foo(void undefined_param) { if(typeof(undefined_param) == int) {
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章PDO版本问题 Invalid parameter number: no
Jenkins 管道作业如下所示: 部分 Jenkinsfile(我们使用脚本化管道)是: properties([parameters([string(defaultValue: "", descr
我是一名优秀的程序员,十分优秀!