2012-06-17 1 views
7

Sto utilizzando Spring Security in uno dei miei progetti. L'app web richiede all'utente di accedere. Quindi ho aggiunto alcuni nomi utente e password nel file primavera-sicurezza-context.xml come segue:Come spostare nome utente/password da spring-security-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> 

La mia domanda è, come spostare queste coppie nome utente-password in un file diverso (come file di alcune proprietà) invece di tenerli in spring-security-context.xml? E come leggere il file delle proprietà del file?

risposta

1

È possibile trovare un modo per spostarli in un database o LDAP. Spring Security supporta sicuramente entrambi.

+0

Qualsiasi link/esempio? Scusate, sono praticamente nuovo per la primavera. – Bhushan

13

È possibile memorizzare i nomi utente e le password in un file .properties separato.

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

users.properties dovrebbe avere il seguente formato:

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

Se si desidera memorizzare in un database, vi consiglio di leggere questo articolo: http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

Riferimento: Spring Security In-Memory Authentication

1

Ho provato i modi suggeriti, infine, ho fatto il seguente sembra funzionare bene

Aggiunto questi cambiamenti nel vostro Web XML

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

Aggiungi questi cambiamenti nel vostro xml primavera-sicurezza

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

Aggiungi questi cambiamenti nella vostra contesto di applicazione XML o se si dispone di proprietà-loader xml anche meglio

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

Quindi aggiungere queste modifiche alla risorsa del file di proprietà service.properties

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

Aggiungi questi cambiamenti in voi risorsa che utilizza Jersey

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

Questo funziona per me per l'autenticazione di sicurezza primavera e l'autorizzazione utilizzando le proprietà dei file:

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

Il file abc.properties :

stat.user=xyz 
stat.pwd=xyz 

Il web.xml voce relativa implementazione primavera-sicurezza:

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