Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move username/passwords out of spring-security-context.xml?

I am using Spring Security in one of my project. The web-app requires the user to login. Hence I have added few usernames and passwords in the spring-security-context.xml file as follows:

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

My question is, how to move these username-password pairs to a different file (like some properties file) instead of keeping them in spring-security-context.xml? And how to read that file properties file?

like image 275
Bhushan Avatar asked Jun 17 '12 22:06

Bhushan


4 Answers

You can store the usernames and passwords in a separate .properties file.

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

users.properties should have the following format:

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

If you want to store it in a database, I would recommend you to read this article: http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

Reference: Spring Security In-Memory Authentication

like image 200
Keerthiram Murugesan Avatar answered Nov 16 '22 03:11

Keerthiram Murugesan


You can use the PropertyPlaceholderConfigurer - put them in properties file and then reference them using EL:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer

like image 24
nickdos Avatar answered Nov 16 '22 02:11

nickdos


You can find a way to move them to a database or LDAP. Spring Security surely supports both.

like image 30
duffymo Avatar answered Nov 16 '22 01:11

duffymo


I have tried the suggested ways lastly I did the following seemed to work nicely

Added these changes in your 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> 

Add these changes in your spring-security xml

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

Add these changes into your application context xml or if you have property-loader xml even better

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

Then Add these changes in your property file resourceservice.properties

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

Add these changes in you resource that uses Jersey

@PUT
@Path("{accountId}")
@Consumes("application/xml")
@PreAuthorize("hasRole('AUTHORISED')")
public Response methodName
like image 20
Basil Zocca Avatar answered Nov 16 '22 02:11

Basil Zocca