我想使用spring安全来制作一个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>这个工作很好,唯一的问题是当我点击注销时什么都不做,我仍然拥有在login.Normally之后的权限,它应该返回到登录屏幕,要求用户验证访问该页面。我遗漏了什么?
还有一个我不知道的问题。当我在登录表单上更改时,
<form:form action="${pageContext.request.contextPath}/login" method="POST">至:
<form name='loginform' action="<c:url value='j_spring_security_check' />" method='POST'>我收到一个错误:
HTTP状态404 -/Dr导_2.1/j_spring_security_check
有谁可以帮我?
发布于 2015-07-18 22:12:07
我刚换了security.xml
<http use-expressions="true" disable-url-rewriting="true">
<intercept-url pattern="/listUsers.htm" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/play.htm" access="hasRole('ROLE_USER')" />
<form-login login-page="/login.htm" login-processing-url="/j_spring_security_check"
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.htm" />
</http>已添加
</c:if>
<form method="post"
action="<c:url value='j_spring_security_check' />">并从控制器中移除重定向以注销。现在工作得很好,泰:)
发布于 2015-07-18 07:08:46
在启用CSRF的注销方面,代码中的两个关键位置是:
logout-url="/logout.htm"和
@RequestMapping(value = "/logout.htm", method = RequestMethod.GET)我猜你不需要自己提供注销页面。要执行注销,您需要用户的浏览器执行到/logout.htm的帖子。在这篇文章中,您需要包含您的csrf值。这篇文章指向您配置了注销-url=“/logout.htm”的spring安全终结点,而不是控制器中的注销。
春季文献提供了几个关于如何做到这一点的选项。一种是在用户可以注销的所有页面中包含一个表单,并在用户单击注销菜单链接时使用Javascript提交表单。
如果这样做,您可以删除我前面列出的请求映射。
发布于 2015-07-16 10:17:16
这个工作很好,唯一的问题是当我点击注销时什么也不做。
您只是将用户导航到登录页面。会话不会失效。未清除SecurityContext对象。
您需要使用security /j_spring_security_logout登录用户。
<a href="/j_spring_security_logout">LogOut</a>签出这个示例
对于登录表单,请尝试
<form name='loginform' action="<c:url value='/j_spring_security_check' />" method='POST'>404错误通常是由于上下文路径计算而发生的。
https://stackoverflow.com/questions/31451244
复制相似问题