2011-09-05 10 views
23

मैं वसंत और वसंत सुरक्षा के लिए बहुत नया हूं, और आशा करता हूं कि कोई मेरी समस्या को हल करने में मेरी सहायता कर सकता है।ओपनिड और डाटाबेस एकीकरण के साथ वसंत सुरक्षा

जो मैं प्राप्त करना चाहता हूं वह उपयोगकर्ता के उपयोगकर्ता नाम और ईमेल पते को निकालने के लिए है, इस उपयोगकर्ता को ओपनआईडी प्रदाता (जीमेल) द्वारा सफलतापूर्वक प्रमाणीकृत किया गया है, फिर इस उपयोगकर्ता के लिए usermodel लोड करने के लिए डेटाबेस के साथ जांचें।

मेरी वसंत-security.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" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" 
    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 
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <security:authentication-manager alias="openIDAuthenticationManager" /> 

    <bean id="authenticationSuccessHandler" class="org.school.openid.service.YouEatAuthenticationSuccessHandler"> 
     <property name="defaultTargetUrl" value="/krams/main/common" /> 
     <property name="attributes2UserDetails" ref="openIDAttributes2UserDetails" /> 
    </bean> 

    <security:http > 
     <security:anonymous enabled="false" /> 
     <security:logout /> 
     <security:openid-login user-service-ref="userDetailsServiceOpenIDImpl" authentication-success-handler-ref="authenticationSuccessHandler" 
      login-page="/krams/auth/login" authentication-failure-url="/krams/auth/login?error=true"> 
      <security:attribute-exchange> 
       <security:openid-attribute name="email" type="http://schema.openid.net/contact/email" required="true" /> 
       <security:openid-attribute name="firstName" type="http://axschema.org/namePerson/first" required="true" /> 
       <security:openid-attribute name="lastName" type="http://axschema.org/namePerson/last" required="true" /> 
      </security:attribute-exchange> 
     </security:openid-login> 
    </security:http> 

    <bean id="openIDAttributes2UserDetails" class="org.school.openid.service.OpenIDAttributes2UserDetailsImpl" /> 

    <bean id="userDetailsServiceOpenIDImpl" class="org.school.openid.service.UserDetailsServiceOpenIDImpl" /> 

</beans> 

मेरी समस्या UserDetailsServiceOpenIDImpl.java पर है

 
public class UserDetailsServiceOpenIDImpl implements UserDetailsService { 

    public UserDetails loadUserByUsername(String username) 
      throws UsernameNotFoundException, DataAccessException { 
     System.out.println(username); 
     //extract username and email address, HOW? 
    } 
} 

प्रिंट बयान प्रिंट बाहर की तरह

 
https://www.google.com/accounts/o8/id?id=AItOawlq2C3EdFAuqp-ski_xkgB8jsEKbe-mZE 

कुछ है मेरे प्रश्न

हैं

(1) मैं लौटा यूआरएल से उपयोगकर्ता नाम और ईमेल पता कैसे निकाल सकता हूं (भी, मुझे यह भी यकीन नहीं है कि उपयोगकर्ता नाम और ईमेल पता सही तरीके से लौटाया गया है)?

(2) ग्रहण पर डीबग चलाकर, यूईएट प्रमाणीकरण SUccessHandler को यूआरएल (https://www.google.com/accounts/o8/id?id=AItOawlq2C3EdFAuqp-ski_xkgB8jsEKbe-mZE) वापस नहीं किया जाता है।

धन्यवाद।

संपादित करें: लिंक http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html#ns-openid लिंक के लिए धन्यवाद।

यह कहना है कि "विशेषता मान प्रमाणीकरण की प्रक्रिया के हिस्से के रूप में वापस कर रहे हैं और निम्नलिखित कोड का उपयोग कर बाद में पहुँचा जा सकता है: ..."

मैं

 
OpenIDAuthenticationToken token = (OpenIDAuthenticationToken)SecurityContextHolder.getContext().getAuthentication(); 
List attributes = token.getAttributes(); 

जोड़ लिया है loadUserByUsername विधि में । लेकिन "टोकन" ऑब्जेक्ट शून्य है।

संपादित 2 निम्नलिखित पेज https://fisheye.springsource.org/browse/spring-security/samples/openid/src/main/webapp/WEB-INF/applicationContext-security.xml?hb=true करके, मैं उपयोगकर्ता के लिए नाम और ईमेल पता अतिरिक्त कर रहा हूँ। मेरे वसंत-security.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" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" 
    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 
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 
    <security:authentication-manager alias="openIDAuthenticationManager" /> 
    <security:http pattern="/krams/auth/login" security="none"/> 
    <security:http auto-config="true" access-denied-page="/krams/auth/denied"> 
     <security:intercept-url pattern="/krams/main/*" access="ROLE_USER" /> 
     <security:anonymous enabled="false" /> 
     <security:logout 
      invalidate-session="true" 
      logout-success-url="/krams/auth/login" 
      logout-url="/krams/auth/logout"/> 
     <security:openid-login 
      user-service-ref="registeringUserService" 
      login-page="/krams/auth/login" 
      authentication-failure-url="/krams/auth/login?error=true" 
      default-target-url="/krams/main/common"> 
      <security:attribute-exchange identifier-match="https://www.google.com/.*"> 
       <security:openid-attribute name="email" type="http://schema.openid.net/contact/email" required="true" /> 
       <security:openid-attribute name="firstName" type="http://axschema.org/namePerson/first" required="true" /> 
       <security:openid-attribute name="lastName" type="http://axschema.org/namePerson/last" required="true" /> 
      </security:attribute-exchange> 
      <security:attribute-exchange identifier-match=".*yahoo.com.*"> 
       <security:openid-attribute name="email" type="http://axschema.org/contact/email" required="true"/> 
       <security:openid-attribute name="fullname" type="http://axschema.org/namePerson" required="true" /> 
      </security:attribute-exchange> 
     </security:openid-login> 
     <!-- if remember is needed 
     <security:remember-me token-repository-ref="tokenRepo"/> 
     -->  
    </security:http> 
    <bean id="tokenRepo" class="org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl" /> 
<!-- 
    A custom UserDetailsService which will allow any user to authenticate and "register" their IDs in an internal map 
    for use if they return to the site. This is the most common usage pattern for sites which use OpenID. 
--> 
    <bean id="registeringUserService" class="org.school.openid.service.CustomUserDetailsService" /> 
</beans> 

मेरे CustomUserDetailsService.java

 
public class CustomUserDetailsService implements AuthenticationUserDetailsService { 

    /* 
    private final Map registeredUsers = new HashMap(); 
    */ 
     private static final List DEFAULT_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_USER"); 
    protected static Logger logger = Logger.getLogger("service");  
    /** 
    * Implementation of {@code AuthenticationUserDetailsService} which allows full access to the submitted 
    * {@code Authentication} object. Used by the OpenIDAuthenticationProvider. 
    */ 
    public UserDetails loadUserDetails(OpenIDAuthenticationToken token) { 
     String id = token.getIdentityUrl(); 
     String email = null; 
     String firstName = null; 
     String lastName = null; 
     String fullName = null; 
     List attributes = token.getAttributes(); 
     for (OpenIDAttribute attribute : attributes) { 
      if (attribute.getName().equals("email")) { 
       email = attribute.getValues().get(0); 
      } 
      if (attribute.getName().equals("firstName")) { 
       firstName = attribute.getValues().get(0); 
      } 
      if (attribute.getName().equals("lastName")) { 
       lastName = attribute.getValues().get(0); 
      } 
      if (attribute.getName().equals("fullname")) { 
       fullName = attribute.getValues().get(0); 
      } 
     } 
     if (fullName == null) { 
      StringBuilder fullNameBldr = new StringBuilder(); 
      if (firstName != null) { 
       fullNameBldr.append(firstName); 
      } 
      if (lastName != null) { 
       fullNameBldr.append(" ").append(lastName); 
      } 
      fullName = fullNameBldr.toString(); 
     } 
     CustomUserDetails user = new CustomUserDetails(id,fullName,email, DEFAULT_AUTHORITIES);   
     logger.debug("Set username " + fullName + " email " + email); 
     return user; 
    } 
} 

मेरे CustomUserDetails.java

 
public class CustomUserDetails extends User { 
    private static final long serialVersionUID = 1L; 
    private String email; 
    private String name; 
    public CustomUserDetails(String id,String name, String email,Collection authorities) { 
     super(name, "unused", true,true,true,true,authorities); 
     this.email = email; 
     this.name = name; 
    } 
    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 
} 

और

 
... 
<repository> 
    <id>org.springframework.maven.milestone</id> 
    <name>Spring Maven Milestone Repository</name> 
    <url>http://maven.springframework.org/milestone</url> 
</repository> 
... 
<dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-core</artifactId> 
    <version>3.1.0.RC1</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-web</artifactId> 
    <version>3.1.0.RC1</version> 
    <type>jar</type> 
     <scope>compile</scope> 
</dependency> 
<dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>3.1.0.RC1</version> 
    </dependency> 
     <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-openid</artifactId> 
     <version>3.1.0.RC1</version> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework</groupId> 
<artifactId>spring-webmvc</artifactId> 
<version>3.0.5.RELEASE</version> 
</dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-openid</artifactId> 
     <version>3.1.0.RC1</version> 
     <type>pom</type> 
     <scope>compile</scope> 
    </dependency> 

आशा है कि आप कुछ बचा सकता है पहर।

+5

यदि आप अपने प्रश्न से अपने प्रश्न को तोड़ने और यहां जवाब पोस्ट करने में कोई दिक्कत नहीं करेंगे, तो यह अच्छा होगा। ऐसा लगता है कि आपको अपनी समस्या का समाधान मिला है, लेकिन यह पूरी तरह से दिखाई नहीं दे रहा है। – Makoto

+2

आप इस तरह के कुछ के लिए स्प्रिंग सोशल देखना चाहते हैं। मैं इसे अपनी परियोजनाओं में से एक के लिए उपयोग करता हूं और यह सब कुछ से एकीकृत करने के लिए बहुत आसान है। उनका दस्तावेज काफी अच्छा है और उनके पास गितूब में कुछ उपयोग उदाहरण हैं। –

+0

मैं सहमत हूं, वसंत सामाजिक में देखो। –

उत्तर

1

जैसा कि मैंने देखा है कि प्रश्न टेक्स्ट में स्वयं का जवाब है। मैं बस इसे खींच रहा हूं और इसे उसी समस्या के साथ अन्य डेवलपर्स को स्पष्टता के लिए उत्तर के रूप में पोस्ट कर रहा हूं। मुझे यह समझने में थोड़ी देर लग गई कि उस प्रश्न का उत्तर है!

उपयोगकर्ता ईमेल & नाम तक पहुंचने के लिए, आपको सुरक्षा xml फ़ाइल में नीचे कॉन्फ़िगरेशन जोड़ने की आवश्यकता है।

<security:attribute-exchange identifier-match="https://www.google.com/.*"> 
    <security:openid-attribute name="email" type="http://schema.openid.net/contact/email" required="true" /> 
    <security:openid-attribute name="firstName" type="http://axschema.org/namePerson/first" required="true" /> 
    <security:openid-attribute name="lastName" type="http://axschema.org/namePerson/last" required="true" /> 
</security:attribute-exchange> 
<security:attribute-exchange identifier-match=".*yahoo.com.*"> 
    <security:openid-attribute name="email" type="http://axschema.org/contact/email" required="true"/> 
    <security:openid-attribute name="fullname" type="http://axschema.org/namePerson" required="true" /> 
</security:attribute-exchange> 

इसके बाद, यह, जिन्हें आप नीचे AuthenticationUserDetailsService कक्षा में पहुंचा जा सकेगा।

public UserDetails loadUserDetails(OpenIDAuthenticationToken token) { 
    String id = token.getIdentityUrl(); 
     : 
     : 
    List attributes = token.getAttributes(); 
    for (OpenIDAttribute attribute : attributes) { 
     if (attribute.getName().equals("email")) { 
      email = attribute.getValues().get(0); 
     } 
     if (attribute.getName().equals("firstName")) { 
      firstName = attribute.getValues().get(0); 
     } 
     if (attribute.getName().equals("lastName")) { 
      lastName = attribute.getValues().get(0); 
     } 
     if (attribute.getName().equals("fullname")) { 
      fullName = attribute.getValues().get(0); 
     } 
    } 
     : 
     : 
    // form and return user object 
} 

यह देखते हुए कि हम अब जावा आधारित विन्यास/सेम ​​का अधिक उपयोग करते हैं, जावा आधारित विन्यास करने के लिए इन एक्सएमएल विन्यास का अनुवाद एक समस्या नहीं होनी चाहिए।

उम्मीद है कि यह मदद करता है।

संबंधित मुद्दे