2012-06-17 11 views
7

मैं अपनी परियोजना में से एक में स्प्रिंग सिक्योरिटी का उपयोग कर रहा हूं। वेब-ऐप को उपयोगकर्ता को लॉगिन करने की आवश्यकता होती है। इसलिए मैं कुछ उपयोगकर्ता नाम और वसंत-सुरक्षा-context.xml फ़ाइल में पासवर्ड के रूप में इस जोड़ लिया है:वसंत-सुरक्षा-context.xml से उपयोगकर्ता नाम/पासवर्ड कैसे स्थानांतरित करें?

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="user_1" password="password_1" authorities="ROLE_USER" /> 
      <user name="user_2" password="password_2" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 

मेरा प्रश्न है, एक अलग फाइल करने के लिए इन उपयोगकर्ता नाम-पासवर्ड जोड़े जाने का तरीका (कुछ संपत्तियों की तरह फाइल) वसंत-सुरक्षा-context.xml में रखने के बजाय? और उस फ़ाइल गुण फ़ाइल को कैसे पढ़ा जाए?

उत्तर

1

आप उन्हें डेटाबेस या एलडीएपी में स्थानांतरित करने का कोई तरीका ढूंढ सकते हैं। वसंत सुरक्षा निश्चित रूप से दोनों का समर्थन करता है।

+0

गया कोई भी लिंक/उदाहरण? क्षमा करें, मैं वसंत के लिए काफी नया हूँ। – Bhushan

13

आप एक अलग .properties फ़ाइल में उपयोगकर्ता नाम और पासवर्ड स्टोर कर सकते हैं।

<user-service id="userDetailsService" properties="users.properties"/> 

users.properties निम्न स्वरूप होना चाहिए: http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

संदर्भ:: Spring Security In-Memory Authentication

jimi=jimispassword,ROLE_USER,ROLE_ADMIN,enabled 
bob=bobspassword,ROLE_USER,enabled 

आप एक डेटाबेस में संग्रहीत करना चाहते हैं, मैं इस लेख को पढ़ने के लिए आप की सिफारिश करेंगे

1

मैंने सुझाए गए तरीकों का प्रयास किया है, मैंने आखिरकार निम्नलिखित काम किया है

अपने वेब एक्सएमएल में इन परिवर्तनों जोड़ा

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

<servlet-mapping> 
<servlet-name>service</servlet-name> 
<url-pattern>/*</url-pattern> 
</servlet-mapping> 

<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> 

अपने वसंत-सुरक्षा एक्सएमएल में इन परिवर्तनों जोड़े

<security:authentication-manager alias="authenticationManager"> 
<security:authentication-provider> 
<security:user-service> 
<security:user name="${resource.service.authentication.name}" 
authorities="${resource.service.authentication.authorities}" 
password="${resource.service.authentication.password}"/> 
</security:user-service> 
</security:authentication-provider> 
</security:authentication-manager> 

आपके आवेदन संदर्भ एक्सएमएल में इन परिवर्तनों को जोड़ें या आपके पास प्रॉपर्टी-लोडर एक्सएमएल अगर यहां तक ​​कि बेहतर

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="placeholderPrefix" value="${" /> 
<property name="placeholderSuffix" value="}" /> 
<property name="locations"> 
<list> 
<value>classpath:resourceservice.properties</value> 
</list> 
</property> 
</bean> 

फिर इन गुणों को अपनी संपत्ति फ़ाइल संसाधन में जोड़ें service.properties

memberservice.authentication.name=usename 
memberservice.authentication.authorities=AUTHORISED 
memberservice.authentication.password=password 

आप संसाधन जर्सी

@PUT 
@Path("{accountId}") 
@Consumes("application/xml") 
@PreAuthorize("hasRole('AUTHORISED')") 
public Response methodName 
0

यह वसंत सुरक्षा प्रमाणीकरण और प्राधिकरण गुण उपयोग करने के लिए मेरे लिए काम करता का उपयोग करता है में इन परिवर्तनों जोड़े दायर:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

    <mvc:annotation-driven /> 

    <bean id="webPropertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="ignoreResourceNotFound" value="true" /> 
     <property name="ignoreUnresolvablePlaceholders" value="true" /> 
     <property name="locations"> 
      <list> 
       <value>classpath:abc.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean 
     class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

    <security:http auto-config="true" use-expressions="true"> 
     <security:intercept-url pattern="/stat/login" access="permitAll"/> 
     <security:intercept-url pattern="/stat/summary" access="hasRole('ROLE_ADMIN')" /> 

     <security:form-login login-page="/stat/login" 
      default-target-url="/stat/summary" authentication-failure-url="/stat/loginError" /> 
    </security:http> 
    <!-- Username and password used from xml --> 
    <!-- <security:authentication-manager> 
     <security:authentication-provider> 
      <security:user-service> 
       <security:user name="xyz" password="xyz" authorities="ROLE_ADMIN" /> 
      </security:user-service> 
     </security:authentication-provider> 
    </security:authentication-manager> --> 

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:user-service> 
     <security:user name="${stat.user}" password="${stat.pwd}" authorities="ROLE_ADMIN" /> 
     </security:user-service> 
     </security:authentication-provider> 
    </security:authentication-manager> 
</beans> 

abc.properties फ़ाइल :

stat.user=xyz 
stat.pwd=xyz 

वसंत-सुरक्षा कार्यान्वयन के लिए web.xml प्रविष्टि:

<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> 
संबंधित मुद्दे

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