2012-03-28 10 views
10

ग्राहक इस परिदृश्य पर करना चाहते हैं:PreAuthentication -> URL के आधार पैरामीटर

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

मुझे यह कैसे पता चलेगा ?! क्या पहले से ही कोई समाधान उपलब्ध है?

धन्यवाद!

संबंध मथायस

उत्तर

20

मैंने पहले ही समस्या हल कर दी है। जो लोग रुचि रखते हैं के लिए ....

web.xml

<!-- ===== SPRING CONFIG ===== --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

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

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/applicationContext.xml 
     /WEB-INF/applicationContext-security.xml 
    </param-value> 
</context-param> 

applicationContext.xml

<context:component-scan base-package="at.beko.rainstar2" /> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

applicationContext-security.xml

<!-- Configuring security not finished!! --> 
<http create-session="never" use-expressions="true" auto-config="false" 
    entry-point-ref="preAuthenticatedProcessingFilterEntryPoint"> 
    <intercept-url pattern="/authError.xhtml" access="permitAll" /> 
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> 
    <custom-filter position="PRE_AUTH_FILTER" ref="preAuthFilter" /> 
    <session-management session-fixation-protection="none" /> 
</http> 

<beans:bean id="userDetailsServiceImpl" 
    class="at.beko.rainstar2.service.impl.UserDetailsServiceImpl" /> 

<beans:bean id="preAuthenticatedProcessingFilterEntryPoint" 
    class="at.beko.rainstar2.model.LinkForbiddenEntryPoint" /> 

<beans:bean id="preAuthenticationProvider" 
    class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider"> 
    <beans:property name="preAuthenticatedUserDetailsService" 
     ref="userDetailsServiceImpl" /> 
</beans:bean> 

<beans:bean id="preAuthFilter" 
    class="at.beko.rainstar2.service.filter.UrlParametersAuthenticationFilter"> 
    <beans:property name="authenticationManager" ref="appControlAuthenticationManager" /> 
</beans:bean> 

<authentication-manager alias="appControlAuthenticationManager"> 
    <authentication-provider ref="preAuthenticationProvider" /> 
</authentication-manager> 

LinkForbiddenEntryPoint.java

public class LinkForbiddenEntryPoint implements AuthenticationEntryPoint { 

@Override 
public void commence(HttpServletRequest request, 
     HttpServletResponse response, AuthenticationException authException) 
     throws IOException, ServletException { 
    HttpServletResponse httpResponse = (HttpServletResponse) response; 
    httpResponse.sendRedirect("/rainstar2-webapp/authError.xhtml"); 
} 

}

UrlParametersAuthenticationFilter.java

public class UrlParametersAuthenticationFilter extends 
    AbstractPreAuthenticatedProcessingFilter { 

@Override 
protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) { 
    if (request.getParameterMap().size() == 2) { 
     return true; 
    } 
    return false; 
} 

@Override 
protected Object getPreAuthenticatedCredentials(HttpServletRequest request) { 
    String[] credentials = new String[2]; 
    credentials[0] = request.getParameter("param1"); 
    credentials[1] = request.getParameter("param2"); 
    return credentials; 
} 

}

UserDetailsServiceImpl.java

@SuppressWarnings("deprecation") 
public class UserDetailsServiceImpl implements 
    AuthenticationUserDetailsService<Authentication> { 

@Override 
public UserDetails loadUserDetails(Authentication token) 
     throws UsernameNotFoundException { 
    UserDetails userDetails = null; 

      String[] credentials = (String[]) token.getPrincipal(); 
    boolean principal = Boolean.valueOf(token.getCredentials().toString()); 

    if (credentials != null && principal == true) { 
     String name = credentials[0]; 
     if ("admin".equalsIgnoreCase(name)) { 
      userDetails = getAdminUser(name); 
     } else if ("händler".equalsIgnoreCase(name)) { 
      userDetails = getRetailerUser(name); 
     } else if ("user".equalsIgnoreCase(name)) { 
      userDetails = getUserUser(name); 
     } 
    } 

    if (userDetails == null) { 
     throw new UsernameNotFoundException("Could not load user : " 
       + token.getName()); 
    } 

    return userDetails; 
} 

private UserDetails getAdminUser(String username) { 
    Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(); 
    grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_USER")); 
    grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_RETAILER")); 
    grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_ADMIN")); 
    return new User(username, "notused", true, true, true, true, 
      grantedAuthorities); 
} 

private UserDetails getRetailerUser(String username) { 
    Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(); 
    grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_USER")); 
    grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_RETAILER")); 
    return new User(username, "notused", true, true, true, true, 
      grantedAuthorities); 
} 

private UserDetails getUserUser(String username) { 
    Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(); 
    grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_USER")); 
    return new User(username, "notused", true, true, true, true, 
      grantedAuthorities); 
} 

} टी

+0

मैं कोशिश कर रहा हूँ o अपने समाधान को कार्यान्वित करें लेकिन मैं स्ट्रिंग पर उपयोगकर्ताDetailsServiceImpl कक्षा में सत्य प्राप्त कर रहा हूं [] credentials = (स्ट्रिंग []) token.get प्रिंसिपल(); स्थापित मुझे प्रमाण पत्र प्राप्त करना चाहिए। – focode

+0

इस संबंधित आलेख ने मुझे इसhttps को लागू करने में मदद की: //stackoverflow.com/questions/12478589/springsecurity- कस्टम-automatic- प्रमाणीकरण – encastellano

2

तरह से मैं इसी तरह की परिस्थितियों के साथ इस का समाधान कर लिया मापदंडों हड़पने के लिए एक सर्वलेट फिल्टर का उपयोग करने के लिए है। मैं org.springframework.web.filter.GenericFilterBean विस्तार करने की सिफारिश करेंगे।

इन पैरामीटर से, किसी प्रकार का ऑथ ऑब्जेक्ट बनाएं (जैसे टोकन), जिसे प्रमाणीकरण प्रबंधक में पारित किया जा सकता है जिसे आप ऑटोवायर कर सकते हैं (या किसी अन्य विधि में प्राप्त करें)।

आपको फिर एक प्रमाणीकरण प्रदाता होना चाहिए जो आपके ऑथ ऑब्जेक्ट को संभालने और ग्रिटेड प्राधिकरण संग्रह के साथ उपयोगकर्ता डेटा ऑब्जेक्ट उत्पन्न कर सकता है, जिसे आप उस विशिष्ट भूमिकाओं को पूरा करने के लिए जरूरी हैं जिन्हें आप चाहते हैं।

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