You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by ka...@apache.org on 2010/02/19 08:25:50 UTC

svn commit: r911722 - in /incubator/shiro/trunk/samples/spring-client: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/shiro/ src/main/java/org/apache/shiro/samples/ src/main/java/org/apache/shiro/...

Author: kaosko
Date: Fri Feb 19 07:25:50 2010
New Revision: 911722

URL: http://svn.apache.org/viewvc?rev=911722&view=rev
Log:
SHIRO-89: Sample Spring Application - WebStart won't launch
- Refactor Spring sample client from Spring sample into its own module to make packaging & dependency management easier and simpler. Work in progress

Added:
    incubator/shiro/trunk/samples/spring-client/
    incubator/shiro/trunk/samples/spring-client/pom.xml
    incubator/shiro/trunk/samples/spring-client/src/
    incubator/shiro/trunk/samples/spring-client/src/main/
    incubator/shiro/trunk/samples/spring-client/src/main/java/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/DefaultSampleManager.java
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/SampleManager.java
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartDriver.java
    incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartView.java
    incubator/shiro/trunk/samples/spring-client/src/main/resources/
    incubator/shiro/trunk/samples/spring-client/src/main/resources/logo.png   (with props)
    incubator/shiro/trunk/samples/spring-client/src/main/resources/webstart.spring.xml

Added: incubator/shiro/trunk/samples/spring-client/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/pom.xml?rev=911722&view=auto
==============================================================================
--- incubator/shiro/trunk/samples/spring-client/pom.xml (added)
+++ incubator/shiro/trunk/samples/spring-client/pom.xml Fri Feb 19 07:25:50 2010
@@ -0,0 +1,55 @@
+<?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.
+  -->
+<!--suppress osmorcNonOsgiMavenDependency -->
+<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.samples</groupId>
+        <artifactId>shiro-samples</artifactId>
+        <version>1.0-incubating-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>samples-spring-client</artifactId>
+    <name>Apache Shiro :: Samples :: Spring Client</name>
+    <description>Spring Client used by Spring Sample webapplication</description>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-spring</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>

Added: incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/DefaultSampleManager.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/DefaultSampleManager.java?rev=911722&view=auto
==============================================================================
--- incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/DefaultSampleManager.java (added)
+++ incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/DefaultSampleManager.java Fri Feb 19 07:25:50 2010
@@ -0,0 +1,106 @@
+/*
+ * 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.samples.spring;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.session.Session;
+import org.apache.shiro.subject.Subject;
+
+
+/**
+ * Default implementation of the {@link SampleManager} interface that stores
+ * and retrieves a value from the user's session.
+ *
+ * @author Jeremy Haile
+ * @since 0.1
+ */
+public class DefaultSampleManager implements SampleManager {
+
+    /*--------------------------------------------
+    |             C O N S T A N T S             |
+    ============================================*/
+    /**
+     * Key used to store the value in the user's session.
+     */
+    private static final String VALUE_KEY = "sample_value";
+
+    /*--------------------------------------------
+    |    I N S T A N C E   V A R I A B L E S    |
+    ============================================*/
+    private static final Logger log = LoggerFactory.getLogger(DefaultSampleManager.class);
+
+    /*--------------------------------------------
+    |         C O N S T R U C T O R S           |
+    ============================================*/
+
+    /*--------------------------------------------
+    |  A C C E S S O R S / M O D I F I E R S    |
+    ============================================*/
+
+    /*--------------------------------------------
+    |               M E T H O D S               |
+    ============================================*/
+
+    public String getValue() {
+        String value = null;
+
+        Subject subject = SecurityUtils.getSubject();
+        Session session = subject.getSession(false);
+        if (session != null) {
+            value = (String) session.getAttribute(VALUE_KEY);
+            if (log.isDebugEnabled()) {
+                log.debug("retrieving session key [" + VALUE_KEY + "] with value [" + value + "] on session with id [" + session.getId() + "]");
+            }
+        }
+
+        return value;
+    }
+
+    public void setValue(String newValue) {
+        Subject subject = SecurityUtils.getSubject();
+        Session session = subject.getSession();
+
+        if (log.isDebugEnabled()) {
+            log.debug("saving session key [" + VALUE_KEY + "] with value [" + newValue + "] on session with id [" + session.getId() + "]");
+        }
+
+        session.setAttribute(VALUE_KEY, newValue);
+    }
+
+    public void secureMethod1() {
+        if (log.isInfoEnabled()) {
+            log.info("Secure method 1 called...");
+        }
+    }
+
+    public void secureMethod2() {
+        if (log.isInfoEnabled()) {
+            log.info("Secure method 2 called...");
+        }
+    }
+
+    public void secureMethod3() {
+        if (log.isInfoEnabled()) {
+            log.info("Secure method 3 called...");
+        }
+    }
+}

Added: incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/SampleManager.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/SampleManager.java?rev=911722&view=auto
==============================================================================
--- incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/SampleManager.java (added)
+++ incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/SampleManager.java Fri Feb 19 07:25:50 2010
@@ -0,0 +1,65 @@
+/*
+ * 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.samples.spring;
+
+import org.apache.shiro.authz.annotation.RequiresPermissions;
+import org.apache.shiro.authz.annotation.RequiresRoles;
+
+
+/**
+ * Business manager interface used for sample application.
+ *
+ * @author Jeremy Haile
+ * @since 0.1
+ */
+public interface SampleManager {
+
+    /**
+     * Returns the value stored in the user's session.
+     *
+     * @return the value.
+     */
+    String getValue();
+
+
+    /**
+     * Sets a value to be stored in the user's session.
+     *
+     * @param newValue the new value to store in the user's session.
+     */
+    void setValue(String newValue);
+
+    /**
+     * Method that requires <tt>role1</tt> in order to be invoked.
+     */
+    @RequiresRoles("role1")
+    void secureMethod1();
+
+    /**
+     * Method that requires <tt>role2</tt> in order to be invoked.
+     */
+    @RequiresRoles("role2")
+    void secureMethod2();
+
+    /**
+     * Method that requires <tt>permission1</tt> in order to be invoked.
+     */
+    @RequiresPermissions("permission2")
+    void secureMethod3();
+}

Added: incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartDriver.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartDriver.java?rev=911722&view=auto
==============================================================================
--- incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartDriver.java (added)
+++ incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartDriver.java Fri Feb 19 07:25:50 2010
@@ -0,0 +1,36 @@
+/*
+ * 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.samples.spring.ui;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Driver class used to launch the web start application by loading a
+ * Spring application context.  Once the Spring application context is
+ * loaded, the initialization of the {@link WebStartView} does the rest.
+ *
+ * @author Jeremy Haile
+ * @since 0.1
+ */
+public class WebStartDriver {
+
+    public static void main(String[] args) {
+        new ClassPathXmlApplicationContext("webstart.spring.xml");
+    }
+}

Added: incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartView.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartView.java?rev=911722&view=auto
==============================================================================
--- incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartView.java (added)
+++ incubator/shiro/trunk/samples/spring-client/src/main/java/org/apache/shiro/samples/spring/ui/WebStartView.java Fri Feb 19 07:25:50 2010
@@ -0,0 +1,168 @@
+/*
+ * 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.samples.spring.ui;
+
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import javax.swing.*;
+
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.core.io.ClassPathResource;
+
+import org.apache.shiro.authz.AuthorizationException;
+import org.apache.shiro.samples.spring.DefaultSampleManager;
+import org.apache.shiro.samples.spring.SampleManager;
+
+
+/**
+ * Simple web start application that helps to demo single sign-on and
+ * remoting authorization using Shiro.  The injected <tt>SampleManager</tt>
+ * is hosted by the Spring sample web application and remotely invoked
+ * when the buttons in this view are clicked.
+ *
+ * @author Jeremy Haile
+ * @since 0.1
+ */
+public class WebStartView implements ActionListener, InitializingBean {
+
+    /*--------------------------------------------
+    |             C O N S T A N T S             |
+    ============================================*/
+
+    /*--------------------------------------------
+    |    I N S T A N C E   V A R I A B L E S    |
+    ============================================*/
+    private SampleManager sampleManager;
+    private JTextField valueField;
+    private JButton saveButton;
+    private JButton refreshButton;
+    private JButton secureMethod1Button;
+    private JButton secureMethod2Button;
+    private JButton secureMethod3Button;
+    private JFrame frame;
+
+    /*--------------------------------------------
+    |         C O N S T R U C T O R S           |
+    ============================================*/
+
+    /*--------------------------------------------
+    |  A C C E S S O R S / M O D I F I E R S    |
+    ============================================*/
+
+    public void setSampleManager(SampleManager sampleManager) {
+        this.sampleManager = sampleManager;
+    }
+
+    /*--------------------------------------------
+    |               M E T H O D S               |
+    ============================================*/
+    public void afterPropertiesSet() throws Exception {
+        ClassPathResource resource = new ClassPathResource("logo.png");
+        ImageIcon icon = new ImageIcon(resource.getURL());
+        JLabel logo = new JLabel(icon);
+
+        valueField = new JTextField(20);
+        updateValueLabel();
+
+        saveButton = new JButton("Save Value");
+        saveButton.addActionListener(this);
+
+        refreshButton = new JButton("Refresh Value");
+        refreshButton.addActionListener(this);
+
+        JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        valuePanel.add(valueField);
+        valuePanel.add(saveButton);
+        valuePanel.add(refreshButton);
+
+        secureMethod1Button = new JButton("Method #1");
+        secureMethod1Button.addActionListener(this);
+
+        secureMethod2Button = new JButton("Method #2");
+        secureMethod2Button.addActionListener(this);
+
+        secureMethod3Button = new JButton("Method #3");
+        secureMethod3Button.addActionListener(this);
+
+        JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        methodPanel.add(secureMethod1Button);
+        methodPanel.add(secureMethod2Button);
+        methodPanel.add(secureMethod3Button);
+
+        frame = new JFrame("Apache Shiro Sample Application");
+        frame.setSize(500, 200);
+
+        Container panel = frame.getContentPane();
+        panel.setLayout(new BorderLayout());
+        panel.add(logo, BorderLayout.NORTH);
+        panel.add(valuePanel, BorderLayout.CENTER);
+        panel.add(methodPanel, BorderLayout.SOUTH);
+
+        frame.setVisible(true);
+        frame.addWindowListener(new WindowAdapter() {
+            public void windowClosing(WindowEvent e) {
+                System.exit(0);
+            }
+        });
+    }
+
+    private void updateValueLabel() {
+        valueField.setText(sampleManager.getValue());
+    }
+
+    public void actionPerformed(ActionEvent e) {
+        try {
+
+            if (e.getSource() == saveButton) {
+                sampleManager.setValue(valueField.getText());
+
+            } else if (e.getSource() == refreshButton) {
+                updateValueLabel();
+
+            } else if (e.getSource() == secureMethod1Button) {
+                sampleManager.secureMethod1();
+                JOptionPane.showMessageDialog(frame, "Method #1 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
+
+            } else if (e.getSource() == secureMethod2Button) {
+                sampleManager.secureMethod2();
+                JOptionPane.showMessageDialog(frame, "Method #2 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
+            } else if (e.getSource() == secureMethod3Button) {
+                sampleManager.secureMethod3();
+                JOptionPane.showMessageDialog(frame, "Method #3 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
+
+            } else {
+                throw new RuntimeException("Unexpected action event from source: " + e.getSource());
+            }
+
+        } catch (AuthorizationException ae) {
+            JOptionPane.showMessageDialog(frame, "Unauthorized to perform action: " + ae.getMessage(), "Unauthorized", JOptionPane.WARNING_MESSAGE);
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        WebStartView test = new WebStartView();
+        test.setSampleManager(new DefaultSampleManager());
+        test.afterPropertiesSet();
+    }
+
+
+}

Added: incubator/shiro/trunk/samples/spring-client/src/main/resources/logo.png
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/src/main/resources/logo.png?rev=911722&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/shiro/trunk/samples/spring-client/src/main/resources/logo.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/shiro/trunk/samples/spring-client/src/main/resources/webstart.spring.xml
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/samples/spring-client/src/main/resources/webstart.spring.xml?rev=911722&view=auto
==============================================================================
--- incubator/shiro/trunk/samples/spring-client/src/main/resources/webstart.spring.xml (added)
+++ incubator/shiro/trunk/samples/spring-client/src/main/resources/webstart.spring.xml Fri Feb 19 07:25:50 2010
@@ -0,0 +1,42 @@
+<?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.
+  -->
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<!--
+  - Application context for Shiro WebStart sample application
+  -->
+<beans>
+
+  <bean id="webStartView"
+        class="org.apache.shiro.samples.spring.ui.WebStartView">
+    <property name="sampleManager" ref="sampleManager"/>
+  </bean>
+
+  <bean id="sampleManager"
+        class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
+    <property name="serviceUrl" value="http://localhost:8080/shiro-spring/remoting/sampleManager"/>
+    <property name="serviceInterface" value="org.apache.shiro.samples.spring.SampleManager"/>
+    <property name="remoteInvocationFactory" ref="secureRemoteInvocationFactory"/>
+  </bean>
+
+  <bean id="secureRemoteInvocationFactory"
+        class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>
+
+</beans>