java – Spring安全登录和注销

weixin_38083228 2019-09-12 12:45:06
我想使用spring security创建一个Web应用程序,但我觉得有些不对劲或缺失. 这是我的代码: security.xml文件: <beans:beans xmlns="http://www.springframework.org/schema/security" 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"> <http use-expressions="true" disable-url-rewriting="true"> <intercept-url pattern="/index.htm" access="hasRole('ROLE_ADMIN')" /> <form-login login-processing-url="/login" login-page="/login.htm" username-parameter="userName" password-parameter="password" default-target-url="/index.htm" always-use-default-target="true" authentication-failure-url="/login.htm?auth=fail" /> <logout logout-url="/logout.htm" logout-success-url="/login?out=1" delete-cookies="JSESSIONID" invalidate-session="true" /> </http> <authentication-manager> <authentication-provider> <user-service> <user name="drs" password="123456" authorities="ROLE_ADMIN" /> <user name="dr" password="123456" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans> 控制器: @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的: <form:form action="${pageContext.request.contextPath}/login" method="POST"> <c:if test="${not empty param.err}"> <div> <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" /> </div> </c:if> <c:if test="${not empty param.out}"> <div>You've logged out successfully.</div> </c:if> <c:if test="${not empty param.time}"> <div>You've been logged out due to inactivity.</div> </c:if> Username:<br> <input type="text" name="userName" value="" /> <br> <br> Password:<br> <input type="password" name="password" value="" /> <input value="Login" name="submit" type="submit" /> </form:form> logout.jsp: <a href="${pageContext.request.contextPath}/login.htm">Login</a> 这项工作很好,唯一的问题是当我点击登出时什么都没做,我仍然拥有登录后的权限.通常它应该回到登录界面,要求用户进行身份验证以访问该页面.我错过了什么? 还有一个我无法想象的问题.当我在登录表单中更改时: <form:form action="${pageContext.request.contextPath}/login" method="POST"> 至: <form name='loginform' action="<c:url value='j_spring_security_check' />" method='POST'> 我收到一个错误: HTTP Status 404 – /Dronomy_2.1/j_spring_security_check 谁能帮我?
...全文
7 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38088019 2019-09-12
  • 打赏
  • 举报
回复
在启用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提交表单. 如果这样做,您可以删除我上面列出的请求映射.

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧