2013-04-29 212 views
0

應用程序應該只允許ROLE_ADMIN用戶訪問/祕密地址,但它不能訪問任何人(授權或非授權的用戶),另一個問題是,即使我輸入它允許登錄的錯誤用戶名和密碼,並且不會重定向到error.page。彈簧安全不能正常工作

Web.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name> 
     <param-value>/WEB-INF/tiles.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> 
    </listener> 
     <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
      /WEB-INF/medics-security.xml 
      /WEB-INF/login-service.xml 
     </param-value> 
    </context-param> 
    <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> 
    <filter> 
     <filter-name>struts2</filter-name> 
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

醫護人員-security.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<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.xsd'> 

<beans:import resource='login-service.xml'/> 
<http auto-config="true" access-denied-page="/error.jsp"> 
    <intercept-url pattern="/register*" access="ROLE_ADMIN" /> 
    <intercept-url pattern="/secret*" access="ROLE_ADMIN" /> 
    <form-login login-page="/login.html" authentication-failure-url="/login?error=true"/> 
    <remember-me/> 
    <logout/> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="admin" password="secret" authorities="ROLE_ADMIN"/> 
      <user name="user" password="secret" authorities="ROLE_USER"/> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 
</beans:beans> 

的applicationContext.xml

<?xml version='1.0' encoding='UTF-8'?> 
    <beans xmlns='http://www.springframework.org/schema/beans' 
       xmlns:context='http://www.springframework.org/schema/context' 
       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/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd'> 


    <context:component-scan base-package='com.myproject'/> 
    <bean id='internalResourceResolver' 
       class='org.springframework.web.servlet.view.InternalResourceViewResolver'> 
     <property name='prefix' value='/Web Pages/'/> 
     <property name='suffix' value='.jsp'/> 
    </bean> 
    <bean 
    class='org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'/> 
    <bean class='org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter'/> 
    <bean id='placeholderConfig' 
       class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'/> 

    <bean id="viewResolver" 
    class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass"> 
     <value> 
      org.springframework.web.servlet.view.tiles2.TilesView 
     </value> 
    </property> 
    </bean> 
    <bean id="tilesConfigurer" 
     class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
    <property name="definitions"> 
     <list> 
      <value>/WEB-INF/tiles.xml</value> 
     </list> 
    </property> 
</bean> 
</beans> 

j_spring_security_check.java

public class j_spring_security_check { 

    public String execute(){ 
     System.out.append("here in spring check.java"); 
     return "SUCCESS"; 
    } 
} 

struts.xml的

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <!-- Configuration for the default package. --> 
    <constant name="struts.action.extension" value="html"/> 
    <constant name="struts.enable.SlashesInActionNames" value="true"/> 
    <package name="default" namespace="/" extends="struts-default"> 
     <result-types> 
      <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> 
     </result-types> 


     <action name="*"> 
      <result type="tiles">{1}</result> 
     </action> 

     <action name="j_spring_security_check" 
     class="com.myproject.struts.j_spring_security_check"> 
      <result name="SUCCESS" type="tiles">register</result> 
     </action> 

    </package> 

</struts> 

register.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>secret page</title> 
    </head> 
    <body> 
     <p>register</p> 
     <a href="secret.jsp">secret</a> 
    </body> 
</html> 

回答

1

嘗試刪除此代碼:

<action name="j_spring_security_check" 
class="com.myproject.struts.j_spring_security_check"> 
    <result name="SUCCESS" type="tiles">register</result> 
</action> 

from struts.xml。通常你不需要處理這個URL。 Spring Security具有內置的過濾器負責處理證書。

嘗試通過

<intercept-url pattern="/secret.jsp*" access="ROLE_ADMIN" /> 

,如果你有一個secret.jsp頁,以取代

<intercept-url pattern="/secret*" access="ROLE_ADMIN" /> 

模式。