You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by ad...@apache.org on 2010/10/31 16:09:18 UTC

svn commit: r1029376 - in /shiro/sandbox/struts2: ./ support/ support/struts2/ support/struts2/src/ support/struts2/src/main/ support/struts2/src/main/java/ support/struts2/src/main/java/org/ support/struts2/src/main/java/org/apache/ support/struts2/sr...

Author: adc
Date: Sun Oct 31 15:09:17 2010
New Revision: 1029376

URL: http://svn.apache.org/viewvc?rev=1029376&view=rev
Log:
SHIRO-210 Added interceptor to inject Subject into value stack

Added:
    shiro/sandbox/struts2/support/struts2/
    shiro/sandbox/struts2/support/struts2/pom.xml
    shiro/sandbox/struts2/support/struts2/src/
    shiro/sandbox/struts2/support/struts2/src/main/
    shiro/sandbox/struts2/support/struts2/src/main/java/
    shiro/sandbox/struts2/support/struts2/src/main/java/org/
    shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/
    shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/
    shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/struts2/
    shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/struts2/CurrentShiroUserInterceptor.java
    shiro/sandbox/struts2/support/struts2/src/test/
    shiro/sandbox/struts2/support/struts2/src/test/java/
    shiro/sandbox/struts2/support/struts2/src/test/java/org/
    shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/
    shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/
    shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/struts2/
    shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/struts2/CurrentShiroUserInterceptorTest.java
Modified:
    shiro/sandbox/struts2/pom.xml
    shiro/sandbox/struts2/support/pom.xml

Modified: shiro/sandbox/struts2/pom.xml
URL: http://svn.apache.org/viewvc/shiro/sandbox/struts2/pom.xml?rev=1029376&r1=1029375&r2=1029376&view=diff
==============================================================================
--- shiro/sandbox/struts2/pom.xml (original)
+++ shiro/sandbox/struts2/pom.xml Sun Oct 31 15:09:17 2010
@@ -68,6 +68,7 @@
         <slf4j.version>1.5.6</slf4j.version>
         <hsqldb.version>1.8.0.7</hsqldb.version>
         <jetty.version>6.1.21</jetty.version>
+        <struts2.version>2.1.8.1</struts2.version>
         <spring.version>2.5.6</spring.version>
         <groovy.version>1.6.2</groovy.version>
     </properties>
@@ -453,6 +454,11 @@
                 <optional>true</optional>
             </dependency>
             <dependency>
+                <groupId>org.apache.struts</groupId>
+                <artifactId>struts2-core</artifactId>
+                <version>${struts2.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>org.hibernate</groupId>
                 <artifactId>hibernate-annotations</artifactId>
                 <version>3.2.1.ga</version>

Modified: shiro/sandbox/struts2/support/pom.xml
URL: http://svn.apache.org/viewvc/shiro/sandbox/struts2/support/pom.xml?rev=1029376&r1=1029375&r2=1029376&view=diff
==============================================================================
--- shiro/sandbox/struts2/support/pom.xml (original)
+++ shiro/sandbox/struts2/support/pom.xml Sun Oct 31 15:09:17 2010
@@ -36,6 +36,7 @@
         <module>groovy</module>
         <module>quartz</module>
         <module>spring</module>
+        <module>struts2</module>
     </modules>
 
 </project>

Added: shiro/sandbox/struts2/support/struts2/pom.xml
URL: http://svn.apache.org/viewvc/shiro/sandbox/struts2/support/struts2/pom.xml?rev=1029376&view=auto
==============================================================================
--- shiro/sandbox/struts2/support/struts2/pom.xml (added)
+++ shiro/sandbox/struts2/support/struts2/pom.xml Sun Oct 31 15:09:17 2010
@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <parent>
+        <groupId>org.apache.shiro</groupId>
+        <artifactId>shiro-root</artifactId>
+        <version>1.1.0-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>shiro-struts2</artifactId>
+    <name>Apache Shiro :: Support :: Struts 2</name>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.struts</groupId>
+            <artifactId>struts2-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <!-- Test dependencies -->
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>jcl-over-slf4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-SymbolicName>org.apache.shiro.struts2</Bundle-SymbolicName>
+                        <Export-Package>org.apache.shiro.struts2*;version=${project.version}</Export-Package>
+                        <Import-Package>
+                            org.apache.shiro*;version="${shiro.osgi.importRange}",
+                            org.aopalliance*;version="[1.0.0, 2.0.0)",
+                            org.springframework*;version="[2.5.0, 4.0.0)",
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

Added: shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/struts2/CurrentShiroUserInterceptor.java
URL: http://svn.apache.org/viewvc/shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/struts2/CurrentShiroUserInterceptor.java?rev=1029376&view=auto
==============================================================================
--- shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/struts2/CurrentShiroUserInterceptor.java (added)
+++ shiro/sandbox/struts2/support/struts2/src/main/java/org/apache/shiro/struts2/CurrentShiroUserInterceptor.java Sun Oct 31 15:09:17 2010
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.shiro.struts2;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.interceptor.Interceptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.subject.Subject;
+
+
+/**
+ * Inserts the current Shiro user into the value stack so that it can be
+ * injected into Struts 2 actions should they have a JavaBeans setter
+ * <code>setShiroUser(org.apache.shiro.subject.Subject shiroUser)</code>.
+ *
+ * @version $Revision: $ $Date: $
+ */
+public class CurrentShiroUserInterceptor implements Interceptor {
+
+    private static transient final Logger log = LoggerFactory.getLogger(CurrentShiroUserInterceptor.class);
+
+    public void destroy() {
+    }
+
+    public void init() {
+    }
+
+    public String intercept(ActionInvocation actionInvocation) throws Exception {
+
+        Subject shiroUser = SecurityUtils.getSubject();
+
+        actionInvocation.getStack().setValue("shiroUser", shiroUser);
+
+        return actionInvocation.invoke();
+    }
+}

Added: shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/struts2/CurrentShiroUserInterceptorTest.java
URL: http://svn.apache.org/viewvc/shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/struts2/CurrentShiroUserInterceptorTest.java?rev=1029376&view=auto
==============================================================================
--- shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/struts2/CurrentShiroUserInterceptorTest.java (added)
+++ shiro/sandbox/struts2/support/struts2/src/test/java/org/apache/shiro/struts2/CurrentShiroUserInterceptorTest.java Sun Oct 31 15:09:17 2010
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.shiro.struts2;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.util.ValueStack;
+import org.junit.Test;
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.mgt.DefaultSecurityManager;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+public class CurrentShiroUserInterceptorTest {
+
+    /**
+     * Test to make sure that the interceptor placed the Subject into the
+     * Struts2 value stack.
+     *
+     * @throws Exception if there is an error performing the test
+     */
+    @Test
+    public void testInjection() throws Exception {
+
+        SecurityUtils.setSecurityManager(new DefaultSecurityManager());
+        ValueStack stack = createNiceMock(ValueStack.class);
+        stack.setValue("shiroUser", SecurityUtils.getSubject());
+
+        ActionInvocation ia = createMock(ActionInvocation.class);
+        expect(ia.getStack()).andReturn(stack).atLeastOnce();
+        expect(ia.invoke()).andReturn("TEST_RESULT").anyTimes();
+
+        replay(ia);
+
+        CurrentShiroUserInterceptor csui = new CurrentShiroUserInterceptor();
+
+        assertEquals("TEST_RESULT", csui.intercept(ia));
+
+        verify(ia);
+    }
+}