我开发了使用Spring Security的默认登录页面的应用程序。不过,我想实现我自己的登录页面。我会把一个login.html而不是一个jsp页面。我想为它使用JQuery。我检查了很多例子,但无法实现。我是新来春春的安全,我使用Spring Security 3. 任何想法,哪些步骤我应该遵循?Spring Security的登录页
回答
有在Spring Security的自定义登录页面了四项要求:
- 有一个名为
j_username
输入字段将包含用于认证证书的名称。 - 有一个名为
j_password
输入字段将包含用于认证证书的密码。 - 这些值为
POST
ed的url匹配Spring安全配置中form-login
元素的login-processing-url
属性中定义的url。 - 自定义登录表单的位置必须在
form-login
元素在你的春季安全配置login-page
属性来指定。
的login.html
<body>
<form action="/j_spring_security_check" method="POST">
<label for="username">User Name:</label>
<input id="username" name="j_username" type="text"/>
<label for="password">Password:</label>
<input id="password" name="j_password" type="password"/>
<input type="submit" value="Log In"/>
</form>
</body>
春季安全配置文件
<http use-expressions="true">
<intercept-url pattern="/login*" access="isAnonymous()"/>
<intercept-url pattern="/**" access="isFullyAuthenticated()"/>
<form-login
login-page="/login.html"
login-processing-url="/j_spring_security_check.action"
authentication-failure-url="/login_error.html"
default-target-url="/home.html"
always-use-default-target="true"/>
</http>
我应该写一LoginLogoutController或类似的东西,我怎么会设置是否成功的一个用户登录重定向到哪里? – kamaci
感谢您的回答。我用萤火虫调试它,它说:http:// localhost:8080/j_spring_security_check 302临时移动以解决该重定向问题。 – kamaci
如果你已经在'web.xml'中正确注册了'springSecurityFilterChain',并且在其他任何过滤器之前注册了'springSecurityFilterChain',它应该接受对'login-processing-url'指定的URL的任何请求。 –
我已经工作了几天对我的项目和配置实施春季安全终于做到了为以下几点:
弹簧的security.xml
<security:http auto-config="true" disable-url-rewriting="true" use-expressions="true">
<security:form-login
login-page="/login.html"
login-processing-url="/j_spring_security_check.action"
default-target-url="/index.html"
always-use-default-target="true"
authentication-failure-url="/login.html?error=true" />
<security:intercept-url pattern="/login*" access="isAnonymous()" />
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="select username, password, enabled from smartcaldb.users where username=?"
authorities-by-username-query="select u.username, r.authority from smartcaldb.users u, smartcaldb.roles r where u.userid = r.userid and u.username =?" />
</security:authentication-provider>
</security:authentication-manager>
弹簧-config.xml中
<mvc:annotation-driven />
<context:component-scan base-package="com.smartcal.**" />
<!-- setup database connectivity bean -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<context:property-placeholder location="/WEB-INF/jdbc.properties" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
的web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-config.xml
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/login</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>403</error-code>
<location>/403</location>
</error-page>
的login.html
<body>
<form action="/smartcal/j_spring_security_check.action" method="POST">
<label for="username">User Name:</label>
<input id="username" name="j_username" type="text" />
<label for="password">Password:</label>
<input id="password" name="j_password" type="password" />
<input type="submit" value="Log In" />
</form>
</body>
用于注销使用URL - “/ {} yourAppPathInTheContainer/j_spring_security_logout”
- 1. Facebook登录Spring Security
- 2. Spring Security Servlet登录
- 3. Spring Security 3:多个登录页面
- 4. Spring Security禁用默认登录页面
- 5. grails spring security登录页面显示
- 6. Spring Security没有显示登录页面
- 7. Spring Security表单登录
- 8. Grails + Spring Security单场登录
- 9. Spring Security登录失败
- 10. Spring Security登录问题
- 11. Spring Security Max登录会话
- 12. Spring Boot + Spring Security使用AngularJS登录
- 13. 使用Spring Security录制登录
- 14. Spring Security中的程序化登录2
- 15. Spring Security认证简单的登录
- 16. 如何访问Spring Security的登录页面中的资源包?
- 17. Spring - 将Spring元素(复选框)添加到Spring登录页面(使用Spring-security)
- 18. 集成一个JSF的登录页面使用Spring Security
- 19. Spring Security登录页面与SPA中的React Router重定向
- 20. sitemesh + spring security:显示主装饰页面中的登录用户!
- 21. Spring Security的Grails中重定向到HTTP登录页面
- 22. 使用Spring Security登录登录时的用户代理信息
- 23. Spring Security:绕过登录表格
- 24. Spring-Security-Facebook插件,Facebook登录错误
- 25. 防止重定向到Spring Security登录
- 26. 听成功登录使用Spring Security
- 27. 我无法通过Spring Security Plugin登录
- 28. grails spring security登录不起作用
- 29. Spring Security多个登录用户失败
- 30. Spring Security - 登录表单 - GWT - Anchor标签
嗨,我试图与'纯HTML +的jQuery +春Security'登录页面了。你有没有想过如何包含Spring Security对CSRF的内置支持。 – smwikipedia