ฉันต้องการสร้างเว็บแอปพลิเคชันโดยใช้ระบบรักษาความปลอดภัยแบบสปริง แต่ฉันรู้สึกว่ามีบางอย่างผิดปกติหรือขาดหายไป
นี่คือรหัสของฉัน:
ความปลอดภัย.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";
}
}
เข้าสู่ระบบ.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="/th${pageContext.request.contextPath}/login.htm">Login</a>
วิธีนี้ใช้ได้ดี ปัญหาเดียวคือเมื่อฉันกดออกจากระบบโดยไม่ทำอะไรเลย ฉันยังคงได้รับอนุญาตตามที่ฉันได้รับหลังจากการเข้าสู่ระบบ โดยปกติแล้วควรจะกลับไปที่หน้าจอเข้าสู่ระบบ โดยขอให้ผู้ใช้ตรวจสอบสิทธิ์เพื่อเข้าถึงหน้านั้น ฉันพลาดอะไรไป?
และอีกปัญหาหนึ่งที่ฉันคิดไม่ออก เมื่อฉันเปลี่ยนแบบฟอร์มการเข้าสู่ระบบ:
<form:form action="${pageContext.request.contextPath}/login" method="POST">
to:
<form name='loginform' action="<c:url value='j_spring_security_check' />" method='POST'>
ฉันได้รับข้อผิดพลาด:
สถานะ HTTP 404 - /Dronomy_2.1/j_spring_security_check
ใครสามารถช่วยฉันได้บ้าง?
/logout.htm
เมื่อคลิกลิงก์ของคุณ โดยพื้นฐานแล้วคุณจะไม่ออกจากระบบเลย คุณสมบัติlogin-processing-url
ของคุณตั้งค่าเป็น/login
ดังนั้นเมื่อส่งแบบฟอร์ม คุณจะต้องส่งไปที่/login
- person M. Deinum   schedule 16.07.2015