Looks like there already is support for OpenID protocol in spring, so it is enough to use it
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 30 May 2009 14:54:31 +0200
changeset 2877f6d8bf0d6c
parent 27 77ddbc4ecf78
child 29 e5e40fd406c1
Looks like there already is support for OpenID protocol in spring, so it is enough to use it
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/LoginController.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/UserDetailsImpl.java
webidor/src/main/webapp/WEB-INF/applicationContext.xml
webidor/src/main/webapp/WEB-INF/dispatcher-servlet.xml
webidor/src/main/webapp/WEB-INF/web.xml
     1.1 --- a/webidor/pom.xml	Sat May 30 08:10:23 2009 +0200
     1.2 +++ b/webidor/pom.xml	Sat May 30 14:54:31 2009 +0200
     1.3 @@ -35,11 +35,6 @@
     1.4        <scope>test</scope>
     1.5      </dependency>
     1.6      <dependency>
     1.7 -      <groupId>org.openid4java</groupId>
     1.8 -      <artifactId>openid4java</artifactId>
     1.9 -      <version>0.9.3</version>
    1.10 -    </dependency>
    1.11 -    <dependency>
    1.12        <groupId>org.springframework</groupId>
    1.13        <artifactId>spring-webmvc</artifactId>
    1.14        <version>2.5</version>
    1.15 @@ -50,44 +45,19 @@
    1.16        <version>2.5</version>
    1.17      </dependency>
    1.18      <dependency>
    1.19 -      <groupId>org.apache.struts</groupId>
    1.20 -      <artifactId>struts-core</artifactId>
    1.21 -      <version>1.3.8</version>
    1.22 -    </dependency>
    1.23 -    <dependency>
    1.24 -      <groupId>org.apache.struts</groupId>
    1.25 -      <artifactId>struts-el</artifactId>
    1.26 -      <version>1.3.8</version>
    1.27 -    </dependency>
    1.28 -    <dependency>
    1.29 -      <groupId>org.apache.struts</groupId>
    1.30 -      <artifactId>struts-extras</artifactId>
    1.31 -      <version>1.3.8</version>
    1.32 -    </dependency>
    1.33 -    <dependency>
    1.34 -      <groupId>org.apache.struts</groupId>
    1.35 -      <artifactId>struts-faces</artifactId>
    1.36 -      <version>1.3.8</version>
    1.37 -    </dependency>
    1.38 -    <dependency>
    1.39 -      <groupId>org.apache.struts</groupId>
    1.40 -      <artifactId>struts-mailreader-dao</artifactId>
    1.41 -      <version>1.3.8</version>
    1.42 -    </dependency>
    1.43 -    <dependency>
    1.44 -      <groupId>org.apache.struts</groupId>
    1.45 -      <artifactId>struts-scripting</artifactId>
    1.46 -      <version>1.3.8</version>
    1.47 -    </dependency>
    1.48 -    <dependency>
    1.49 -      <groupId>org.apache.struts</groupId>
    1.50 -      <artifactId>struts-taglib</artifactId>
    1.51 -      <version>1.3.8</version>
    1.52 -    </dependency>
    1.53 -    <dependency>
    1.54 -      <groupId>org.apache.struts</groupId>
    1.55 -      <artifactId>struts-tiles</artifactId>
    1.56 -      <version>1.3.8</version>
    1.57 +      <groupId>org.springframework.security</groupId>
    1.58 +      <artifactId>spring-security-openid</artifactId>
    1.59 +      <version>2.0.4</version>
    1.60 +      <exclusions>
    1.61 +        <exclusion>
    1.62 +          <artifactId>icu4j</artifactId>
    1.63 +          <groupId>com.ibm.icu</groupId>
    1.64 +        </exclusion>
    1.65 +        <exclusion>
    1.66 +          <artifactId>xml-apis</artifactId>
    1.67 +          <groupId>xml-apis</groupId>
    1.68 +        </exclusion>
    1.69 +      </exclusions>
    1.70      </dependency>
    1.71    </dependencies>
    1.72    <build>
    1.73 @@ -110,3 +80,6 @@
    1.74  </project>
    1.75  
    1.76  
    1.77 +
    1.78 +
    1.79 +
     2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/LoginController.java	Sat May 30 08:10:23 2009 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,141 +0,0 @@
     2.4 -/*
     2.5 - * To change this template, choose Tools | Templates
     2.6 - * and open the template in the editor.
     2.7 - */
     2.8 -
     2.9 -package cz.xelfi.quoridor.webidor;
    2.10 -
    2.11 -import java.util.HashMap;
    2.12 -import java.util.List;
    2.13 -import javax.servlet.http.HttpServletRequest;
    2.14 -import javax.servlet.http.HttpServletResponse;
    2.15 -import javax.servlet.http.HttpSession;
    2.16 -import org.openid4java.consumer.ConsumerManager;
    2.17 -import org.openid4java.consumer.VerificationResult;
    2.18 -import org.openid4java.discovery.Discovery;
    2.19 -import org.openid4java.discovery.DiscoveryInformation;
    2.20 -import org.openid4java.discovery.Identifier;
    2.21 -import org.openid4java.message.AuthRequest;
    2.22 -import org.openid4java.message.AuthSuccess;
    2.23 -import org.openid4java.message.MessageExtension;
    2.24 -import org.openid4java.message.ParameterList;
    2.25 -import org.openid4java.message.ax.AxMessage;
    2.26 -import org.openid4java.message.ax.FetchRequest;
    2.27 -import org.openid4java.message.ax.FetchResponse;
    2.28 -import org.springframework.web.servlet.ModelAndView;
    2.29 -import org.springframework.web.servlet.mvc.AbstractController;
    2.30 -
    2.31 -/**
    2.32 - *
    2.33 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.34 - */
    2.35 -public class LoginController extends AbstractController {
    2.36 -
    2.37 -    public LoginController() {
    2.38 -    }
    2.39 -    
    2.40 -    protected ModelAndView handleRequestInternal(
    2.41 -        HttpServletRequest request,
    2.42 -        HttpServletResponse response
    2.43 -    ) throws Exception {
    2.44 -        String openid = request.getParameter("openid"); // NOI18N
    2.45 -        if (openid != null) {
    2.46 -            StringBuffer ret = request.getRequestURL();
    2.47 -            return discover(openid, ret.toString(), request.getSession());
    2.48 -        }
    2.49 -        String openret = request.getParameter("openid.claimed_id");
    2.50 -        if (openret != null) {
    2.51 -            return login(request, response);
    2.52 -        }
    2.53 -        return new ModelAndView("login");
    2.54 -    }
    2.55 -
    2.56 - // instantiate a ConsumerManager object
    2.57 -    private static ConsumerManager manager;
    2.58 -    private ModelAndView discover(String openid, String returnToUrl, HttpSession session) throws Exception {
    2.59 -        if (manager == null) {
    2.60 -            manager = new ConsumerManager();
    2.61 -        }
    2.62 -
    2.63 -        // build an Identifier instance from the user-supplied identifier
    2.64 -        Identifier identifier = Discovery.parseIdentifier(openid);
    2.65 -
    2.66 -        // perform discovery on the user-supplied identifier
    2.67 -        List discoveries = manager.getDiscovery().discover(identifier);
    2.68 -
    2.69 -        // attempt to associate with an OpenID provider
    2.70 -        // and retrieve one service endpoint for authentication
    2.71 -        DiscoveryInformation discovered = manager.associate(discoveries);
    2.72 -
    2.73 -        // store the discovery information in the user's session
    2.74 -        session.setAttribute("openid-disco", discovered);
    2.75 -
    2.76 -        // Attribute Exchange example: fetching the 'email' attribute
    2.77 -        FetchRequest fetch = FetchRequest.createFetchRequest();
    2.78 -        fetch.addAttribute("email",                         // attribute alias
    2.79 -                "http://schema.openid.net/contact/email",   // type URI
    2.80 -                true);                                      // required
    2.81 -
    2.82 -        // obtain a AuthRequest message to be sent to the OpenID provider
    2.83 -        AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
    2.84 -
    2.85 -        // attach the extension to the authentication request
    2.86 -        //authReq.addExtensionParams(fetch);
    2.87 -
    2.88 -        // Option 2: HTML FORM Redirection
    2.89 -        // Allows payloads > 255 bytes
    2.90 -
    2.91 -        // <FORM action="OpenID Provider's service endpoint">
    2.92 -        // see samples/formredirection.jsp for a JSP example
    2.93 -        HashMap<String,Object> map = new HashMap<String, Object>();
    2.94 -        map.put("OPEndpoint", authReq.getOPEndpoint()); // NOI18N
    2.95 -        map.put("message", authReq.getParameterMap()); // NOI18N
    2.96 -
    2.97 -        // build a HTML FORM with the message parameters
    2.98 -        return new ModelAndView("loginredirection", map);
    2.99 -    }
   2.100 -
   2.101 -    private ModelAndView login(HttpServletRequest request, HttpServletResponse res)
   2.102 -    throws Exception {
   2.103 -        // extract the parameters from the authentication response
   2.104 -        // (which comes in as a HTTP request from the OpenID provider)
   2.105 -        ParameterList response = new ParameterList(request.getParameterMap());
   2.106 -
   2.107 -        // retrieve the previously stored discovery information
   2.108 -        DiscoveryInformation discovered
   2.109 -                = (DiscoveryInformation) request.getSession().getAttribute("openid-disco");
   2.110 -
   2.111 -        // extract the receiving URL from the HTTP request
   2.112 -        StringBuffer receivingURL = request.getRequestURL();
   2.113 -        String queryString = request.getQueryString();
   2.114 -        if (queryString != null && queryString.length() > 0)
   2.115 -            receivingURL.append("?").append(request.getQueryString());
   2.116 -
   2.117 -        // verify the response; ConsumerManager needs to be the same
   2.118 -        // (static) instance used to place the authentication request
   2.119 -        VerificationResult verification = manager.verify(
   2.120 -                receivingURL.toString(),
   2.121 -                response, discovered);
   2.122 -
   2.123 -        HashMap<String,Object> map = new HashMap<String, Object>();
   2.124 -        // examine the verification result and extract the verified identifier
   2.125 -        Identifier verified = verification.getVerifiedId();
   2.126 -        if (verified != null)
   2.127 -        {
   2.128 -            /*
   2.129 -            // Attribute Exchange: retrieving the fetched "email" attribute
   2.130 -            AuthSuccess authSuccess = AuthSuccess.createAuthSuccess(response);
   2.131 -            MessageExtension ext =
   2.132 -                    authSuccess.getExtension(AxMessage.OPENID_NS_AX);
   2.133 -            if (ext != null)
   2.134 -            {
   2.135 -                FetchResponse fetchResp =FetchResponse.createFetchResponse(ext.getParameters());
   2.136 -                String email = fetchResp.getParameterValue("email");
   2.137 -            }
   2.138 -             */
   2.139 -            map.put("id", verified.getIdentifier()); // NOI18N
   2.140 -        }
   2.141 -
   2.142 -        return new ModelAndView("index", map);
   2.143 -    }
   2.144 -}
   2.145 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/UserDetailsImpl.java	Sat May 30 14:54:31 2009 +0200
     3.3 @@ -0,0 +1,53 @@
     3.4 +/*
     3.5 + * To change this template, choose Tools | Templates
     3.6 + * and open the template in the editor.
     3.7 + */
     3.8 +
     3.9 +package cz.xelfi.quoridor.webidor;
    3.10 +
    3.11 +import org.springframework.dao.DataAccessException;
    3.12 +import org.springframework.security.GrantedAuthority;
    3.13 +import org.springframework.security.GrantedAuthorityImpl;
    3.14 +import org.springframework.security.userdetails.UserDetails;
    3.15 +import org.springframework.security.userdetails.UserDetailsService;
    3.16 +import org.springframework.security.userdetails.UsernameNotFoundException;
    3.17 +
    3.18 +/**
    3.19 + *
    3.20 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.21 + */
    3.22 +public class UserDetailsImpl implements UserDetailsService {
    3.23 +
    3.24 +    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    3.25 +        return new UserDetails() {
    3.26 +            public GrantedAuthority[] getAuthorities() {
    3.27 +                return new GrantedAuthority[] { new GrantedAuthorityImpl("ROLE_USER") };
    3.28 +            }
    3.29 +
    3.30 +            public String getPassword() {
    3.31 +                return "ok";
    3.32 +            }
    3.33 +
    3.34 +            public String getUsername() {
    3.35 +                return "jarda";
    3.36 +            }
    3.37 +
    3.38 +            public boolean isAccountNonExpired() {
    3.39 +                return true;
    3.40 +            }
    3.41 +
    3.42 +            public boolean isAccountNonLocked() {
    3.43 +                return true;
    3.44 +            }
    3.45 +
    3.46 +            public boolean isCredentialsNonExpired() {
    3.47 +                return true;
    3.48 +            }
    3.49 +
    3.50 +            public boolean isEnabled() {
    3.51 +                return true;
    3.52 +            }
    3.53 +        };
    3.54 +    }
    3.55 +
    3.56 +}
     4.1 --- a/webidor/src/main/webapp/WEB-INF/applicationContext.xml	Sat May 30 08:10:23 2009 +0200
     4.2 +++ b/webidor/src/main/webapp/WEB-INF/applicationContext.xml	Sat May 30 14:54:31 2009 +0200
     4.3 @@ -4,9 +4,11 @@
     4.4         xmlns:p="http://www.springframework.org/schema/p"
     4.5         xmlns:aop="http://www.springframework.org/schema/aop"
     4.6         xmlns:tx="http://www.springframework.org/schema/tx"
     4.7 +       xmlns:security="http://www.springframework.org/schema/security"
     4.8         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     4.9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    4.10 -       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    4.11 +       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    4.12 +       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
    4.13  
    4.14      <!--bean id="propertyConfigurer"
    4.15            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    4.16 @@ -20,5 +22,20 @@
    4.17            p:password="${jdbc.password}" /-->
    4.18      
    4.19      <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    4.20 -    
    4.21 +
    4.22 +    <security:http>
    4.23 +        <security:intercept-url pattern="/login.html" filters="none"/>
    4.24 +        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    4.25 +        <!-- <security:form-login/> -->
    4.26 +        <security:anonymous/>
    4.27 +        <!-- <security:http-basic/> -->
    4.28 +        <security:logout/>
    4.29 +        <security:remember-me/>
    4.30 +        <security:openid-login/>
    4.31 +    </security:http>
    4.32 +
    4.33 +    <bean id="quoridorUserDetailsService" class="cz.xelfi.quoridor.webidor.UserDetailsImpl"/>
    4.34 +
    4.35 +    <security:authentication-provider user-service-ref='quoridorUserDetailsService'>
    4.36 +    </security:authentication-provider>
    4.37  </beans>
     5.1 --- a/webidor/src/main/webapp/WEB-INF/dispatcher-servlet.xml	Sat May 30 08:10:23 2009 +0200
     5.2 +++ b/webidor/src/main/webapp/WEB-INF/dispatcher-servlet.xml	Sat May 30 14:54:31 2009 +0200
     5.3 @@ -36,8 +36,4 @@
     5.4            p:viewName="index" />
     5.5  
     5.6  
     5.7 -    <bean name="/login.html" class="cz.xelfi.quoridor.webidor.LoginController"/>
     5.8 -
     5.9 -
    5.10 -    
    5.11  </beans>
     6.1 --- a/webidor/src/main/webapp/WEB-INF/web.xml	Sat May 30 08:10:23 2009 +0200
     6.2 +++ b/webidor/src/main/webapp/WEB-INF/web.xml	Sat May 30 14:54:31 2009 +0200
     6.3 @@ -5,6 +5,15 @@
     6.4          <param-name>contextConfigLocation</param-name>
     6.5          <param-value>/WEB-INF/applicationContext.xml</param-value>
     6.6      </context-param>
     6.7 +    <filter>
     6.8 +      <filter-name>springSecurityFilterChain</filter-name>
     6.9 +      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    6.10 +    </filter>
    6.11 +
    6.12 +    <filter-mapping>
    6.13 +      <filter-name>springSecurityFilterChain</filter-name>
    6.14 +      <url-pattern>/*</url-pattern>
    6.15 +    </filter-mapping>
    6.16      <listener>
    6.17          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    6.18      </listener>
    6.19 @@ -24,5 +33,5 @@
    6.20      </session-config>
    6.21      <welcome-file-list>
    6.22          <welcome-file>index.jsp</welcome-file>
    6.23 -        </welcome-file-list>
    6.24 -    </web-app>
    6.25 +    </welcome-file-list>
    6.26 +</web-app>