[英]Login Logout use case in Spring security
I am trying to create login use case using spring security(I am referring this example ). 我正在尝试使用Spring Security创建登录用例(我在参考此示例)。
After entering the username and password if a click login
button, it is requesting for 输入用户名和密码后,如果单击“
login
按钮,则要求输入
j_spring_security_check.htm
url and am getting HTTP Status 404 - for the above URL. 网址,并且获取上述网址的HTTP状态404。
Am using netbeans IDE. 我正在使用netbeans IDE。 I don't know where am going wrong.
我不知道哪里出了问题。 Please Help.
请帮忙。
Here is the contents of web.xml
这是
web.xml
的内容
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is the code to the login form in login.jsp
这是
login.jsp
登录表单的代码
<c:if test="${not empty error}">
<div class="errorblock">
Your login attempt was not successful, try again.<br /> Caused :
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
<form name='f' action="<c:url value='j_spring_security_check.htm' />" method="POST">
<div style="margin-left: 27%">
<input type="text" name="j_username" value="" placeholder="Username" class="login_hint" id="username" title="Username" />
<br/>
<input type="password" name="j_password" value="" placeholder="Password" class="login_hint" id="password" title="Password" />
<br />
<input type ="submit" value ="Login" class ="btn btn-primary loginBtn"/>
</div>
</form>
Here is the contents of applicationContext-security.xml
这是
applicationContext-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-3.0.3.xsd">
<http auto-config="true">
<intercept-url pattern="/admin*" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/admin/home.htm"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="root" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
The UsernamePasswordAuthenticationFilter
intercepts requets sent to /j_spring_security_check
(by default), so most probably you only need to remove the .htm
ending from the action URL in login.jsp
: UsernamePasswordAuthenticationFilter
拦截发送到/j_spring_security_check
j_spring_security_check的记录(默认情况下),因此很可能只需要从login.jsp
的操作URL删除.htm
结尾:
<form name='f' action="<c:url value='j_spring_security_check'/>" method="POST">
Oh well, it seems some stuff is missing from web.xml
as well. 哦,好了,看来
web.xml
也缺少一些东西。 You will need to set up the security filter chain: 您将需要设置安全过滤器链:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Sample</display-name>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml,
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Just have the servlet - url mapping as *.htm and filter-mapping url pattern as * 只需提供servlet-网址映射为* .htm,过滤器映射网址格式为*
Try this. 尝试这个。 It will Work.
它会工作。 hope this helps
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.