内容简介:翻译自:https://stackoverflow.com/questions/36243352/how-to-set-redirection-after-successful-login
-boot-starter-security依赖的spring boot.
我有一个应用程序,将成功登录给予适当的凭据.但是,每当我登录时,我都没有被重定向到任何地方.我该如何配置?
以下是表格:
<form th:action="@{/login}" method="post"> <div><label> User Name : <input type="text" name="username"/> </label></div> <div><label> Password: <input type="password" name="password"/> </label></div> <div><input type="submit" value="Sign In"/></div> </form>
我已经尝试过更改上面的th:action标签,但是我无法使用它.
MvcConfig方法如下:
public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); registry.addViewController("/").setViewName("login"); }
成功登录后定义重定向需要应用于Spring Security,而不是Spring MVC.
th:action定义将处理身份验证请求的Spring Security端点.它没有定义重定向URL.开箱即用,Spring Boot Security将为您提供/ login端点.默认情况下,Spring Security将在登录到您尝试访问的安全资源后重定向.如果您希望始终重定向到特定URL,则可以通过HttpSecurity配置对象强制执行此操作.
假设您使用的是最新版本的Spring Boot,您应该可以使用JavaConfig.
这是一个简单的例子:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserService userService; @Override protected void configure(HttpSecurity http) throws Exception { // the boolean flags force the redirection even though // the user requested a specific secured resource. http.formLogin().defaultSuccessUrl("/success.html", true); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService); } }
请注意,您需要定义一个proprer端点来为/success.html URL提供内容.默认情况下,src / main / resources / public /中可用的静态资源可用于测试目的.我个人宁愿定义一个由Spring MVC Controller服务的安全URL,该服务器提供Thymeleaf的内容.您不希望任何匿名用户能够访问成功页面. Thymeleaf是一些在呈现HTML内容时与Spring Security交互的有用功能.
问候,
丹尼尔
翻译自:https://stackoverflow.com/questions/36243352/how-to-set-redirection-after-successful-login
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Php 单一设备设置(一个账号只能一个设备登录)【原创】
- [CentOS7]redis设置开机启动,设置密码
- hadoop地址配置、内存配置、守护进程设置、环境设置
- beego实现用户未登录跳转到登录页面
- Django实现小程序的登录验证功能,并维护登录态
- 【Python3爬虫】破解时光网登录加密参数并实现模拟登录
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
精通Python设计模式
[荷] Sakis Kasampalis / 夏永锋 / 人民邮电出版社 / 2016-7 / 45.00元
本书分三部分、共16章介绍一些常用的设计模式。第一部分介绍处理对象创建的设计模式,包括工厂模式、建造者模式、原型模式;第二部分介绍处理一个系统中不同实体(类、对象等)之间关系的设计模式,包括外观模式、享元模式等;第三部分介绍处理系统实体之间通信的设计模式,包括责任链模式、观察者模式等。一起来看看 《精通Python设计模式》 这本书的介绍吧!