与apache shiro集成时,struts2无法显示结果页面

时间:2017-07-02 14:53:42

标签: spring struts2 shiro

struts2 2.5.10,spring 4.x,struts2-spring-plugin 2.5.10,shiro 1.4.0, shiro-spring 1.4.0。

web.xml:

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

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>

  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>

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

  <!-- shiro filter mapping has to be first -->
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>

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

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

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

beanx.xml:

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

    <bean name="loginAction" class="example.shiro.action.LoginAction" >

    </bean>

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/login.jsp" />

        <property name="filterChainDefinitions">
            <value>
                /login.jsp = authc
                /logout = logout
                /* = authc
            </value>
        </property>
    </bean>

    <bean id="iniRealm" class="org.apache.shiro.realm.text.IniRealm">
        <property name="resourcePath" value="classpath:shiro.ini" />
    </bean>

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

        <property name="realm" ref="iniRealm" />
    </bean>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

</beans>

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="default" extends="struts-default">

        <action name="list" class="loginAction" method="list">
            <result name="success">/success.jsp</result>
            <result name="error">error.jsp</result>
        </action>

    </package>

</struts>

index.jsp:

<body>
    <s:action name="list" />
</body>

login.jsp看起来像:

<form name="loginform" action="" method="post">
        <table align="left" border="0" cellspacing="0" cellpadding="3">
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" maxlength="30"></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" maxlength="30"></td>
            </tr>
            <tr>
                <td colspan="2" align="left"><input type="checkbox"
                    name="rememberMe"><font size="2">Remember Me</font></td>
            </tr>
            <tr>
                <td colspan="2" align="right"><input type="submit"
                    name="submit" value="Login"></td>
            </tr>
        </table>
    </form> 

LoginAction.list():

public String list() {

        Subject currentUser = SecurityUtils.getSubject();

        if(currentUser.isAuthenticated()) {System.out.println("user : "+currentUser.getPrincipal());
            System.out.println("You are authenticated!");
        } else {
            System.out.println("Hey hacker, hands up!");
        }

        return "success";
    }

shiro.ini:

[users]
root=123,admin
guest=456,guest
frank=789,roleA,roleB

# role name=permission1,permission2,..,permissionN
[roles]
admin=*
roleA=lightsaber:*
roleB=winnebago:drive:eagle5

index.jsp,login.jsp和success.jsp放在webapp下。

我想要的是:进入LoginAction.list()需要进行身份验证,如果登录成功,则运行LoginAction.list()并返回“success”,然后显示success.jsp,其定义为struts动作结果。

现在登录成功后可以执行LoginAction.list(),但是不显示success.jsp,浏览器是空白页面。

为什么?

2 个答案:

答案 0 :(得分:1)

我找到了原因:我在index.jsp中使用了<s:action name="list" />,但是struts doc说如果我们想要查看带有<s:action>的结果页面,那么我们必须设置它的属性{{ 1}}为true,就像executeResult

在我看来,这有点有线,默认情况下该属性应为true。

答案 1 :(得分:0)

有一个示例,您应该如何配置applicationContext.xml with Shiro

 <property name="filterChainDefinitions">
    <value>
        # some example chain definitions:
        /admin/** = authc, roles[admin]
        /** = authc
        # more URL-to-FilterChain definitions here
    </value>
 </property>

/admin/开头的网址受角色admin保护,任何其他网址都不受保护。如果它们不在受保护区域中,则会显示Struts操作和结果JSP。