我想使用spring security创建一个Web应用程序,但我觉得有些不对劲或缺失.
这是我的代码:
security.xml文件:
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
username-parameter="userName" password-parameter="password"
default-target-url="/index.htm" always-use-default-target="true"
authentication-failure-url="/login.htm?auth=fail" />
delete-cookies="JSESSIONID" invalidate-session="true" />
控制器:
@Controller
public class SecurityController {
@RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
public String logoutPage() {
return "logoutPage";
}
@RequestMapping(value = "/login.htm", method = RequestMethod.GET)
public String login() {
return "loginPage";
}
}
login.jsp的:
Username:
Password:
logout.jsp:
这项工作很好,唯一的问题是当我点击登出时什么都没做,我仍然拥有登录后的权限.通常它应该回到登录界面,要求用户进行身份验证以访问该页面.我错过了什么?
还有一个我无法想象的问题.当我在登录表单中更改时:
至:
我收到一个错误:
HTTP Status 404 – /Dronomy_2.1/j_spring_security_check
谁能帮我?
解决方法:
在启用CSRF注销方面,代码中的两个关键位置是:
logout-url="/logout.htm"
和
@RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
我猜你不需要自己提供退出页面.要执行注销,您需要用户的浏览器对/logout.htm执行POST.在此POST中,您需要包含csrf值.此POST是使用logout-url =“/ logout.htm”配置的spring安全端点,而不是控制器中的注销.
spring documentation提供了几种如何实现此目的的选项.一种是在用户可以注销的所有页面中包含一个表单,并在用户单击注销菜单链接时使用Javascript提交表单.
如果这样做,您可以删除我上面列出的请求映射.
标签:java,jsp,spring-mvc,spring,spring-security
来源: https://codeday.me/bug/20190702/1359012.html