Spring安全自定义UserDetailsS​​ervice实现+自定义登录页面

时间:2012-11-01 17:37:21

标签: struts2 spring-security

我试图找出如何使用Spring security 3.1和struts2实现一个简单的应用程序。 Actualy,我想提供一个自定义UserDetailsS​​ervice实现,并提供我自己的登录页面。

虽然我正在处理这个简单的小应用程序超过10天,但我无法使其正常工作......官方文档并未明确说明如何执行此操作。

在下面的配置中,如果我使用Spring安全性提供的默认登录页面,一切正常。当我尝试使用我的时候,即使是我也无法登录 调用loadUserByUsername方法并从数据库返回一个有效的UserDetails,并且我坚持登录页面。

在控制台中,我收到消息:

WARNING: No configuration found for the specified action: '/myApplication/j_spring_security_check' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

所以也许我有命名空间问题?

这是我的代码

Web.xml中

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> /WEB-INF/applicationContext.xml
            /WEB-INF/applicationContext-security.xml </param-value>
    </context-param>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>public/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

struts.xml中

<?xml version="1.0" encoding="UTF-8" ?>

    

<package name="public" namespace="/public" extends="struts-default">
    <action name="login" class="loginAction">
        <result name="success">/secure/welcome.jsp</result>
        <result name="input">login.jsp</result>
    </action>

    <action name="register" class="registerAction">
        <result name="success">confirm_register.jsp</result>
        <result name="input">register.jsp</result>
    </action>
</package>


<package name="secure" namespace="/secure" extends="struts-default">

    <action name="add" class="myApplication.action.UserAction" method="add">
        <result name="success">welcome.jsp</result>
    </action>

    <action name="list" class="myApplication.action.UserAction" method="list">
        <result name="success">list.jsp</result>
    </action>

</package>

的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.1.xsd">

<global-method-security pre-post-annotations="enabled">
    <!-- AspectJ pointcut expression that locates our "post" method and applies 
        security that way <protect-pointcut expression="execution(* bigbank.*Service.post*(..))" 
        access="ROLE_TELLER"/> -->
</global-method-security>

<http pattern="/resources" security="none" />

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/public/*" access="permitAll" />
    <intercept-url pattern="/logout" access="permitAll" />
    <intercept-url pattern="/secure/*"
        access="hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/denied" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/" access="hasRole('ROLE_USER')" />

    <form-login login-page="/login.jsp"
        authentication-failure-url="/login.jsp" />

    <access-denied-handler error-page="/denied" />

    <logout invalidate-session="true" logout-success-url="/logout/success"
        logout-url="/logout" />
</http>

<authentication-manager>
    <authentication-provider user-service-ref="customUserDetailsService" />
</authentication-manager>

的login.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>

<body>
    <h1>Identification</h1>
    <s:form action="/myApplication/j_spring_security_check" method="post">
    <s:actionerror />

        <s:textfield label="Username" name="username"/>
        <s:textfield label="Password" name="password"/>
        <s:submit name="submit" />

    </s:form>

</body>
</html>

任何想法/建议?

1 个答案:

答案 0 :(得分:2)

首先正确使用<s:form>代码http://struts.apache.org/2.x/docs/url.html或使用HTML form代码。基于表单的身份验证的第二个默认spring-security用户名和密码字段为j_usernamej_password。 因此,将JSP改为类似的东西,看看是否有效。

<form action="j_spring_security_check" method="post">
  <table>
    <s:textfield name="j_username" autofocus="autofocus" />
    <s:password name="j_password" />
    <s:submit/>
  </table>
</form>