8

プロジェクトの1つでSpringSecurityを使用しています。Webアプリでは、ユーザーがログインする必要があります。したがって、次のように、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>

私の質問は、これらのユーザー名とパスワードのペアを、spring-security-context.xmlに保持するのではなく、別のファイル(一部のプロパティファイルなど)に移動する方法です。そして、そのファイルプロパティファイルを読み取る方法は?

4

6 に答える 6

15

ユーザー名とパスワードを別の .properties ファイルに保存できます。

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

users.properties の形式は次のとおりです。

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

データベースに保存する場合は、次の記事を読むことをお勧めします: http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

参考:Spring Security インメモリ認証

于 2012-06-17T23:43:40.737 に答える
2

使用できますPropertyPlaceholderConfigurer-それらをプロパティファイルに入れてから、ELを使用してそれらを参照します:

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

于 2012-06-17T23:34:21.583 に答える
1

それらをデータベースまたはLDAPに移動する方法を見つけることができます。SpringSecurityは確かに両方をサポートしています。

于 2012-06-17T22:06:31.450 に答える
1

私は提案された方法を試しましたが、最後に次のことを行いました

これらの変更をウェブ 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> 

これらの変更を 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>

これらの変更をアプリケーション コンテキスト xml に追加するか、プロパティ ローダー xml がある場合はさらに良い

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

次に、これらの変更をプロパティ ファイル resourceservice.properties に追加します。

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

Jersey を使用するリソースにこれらの変更を追加します

@PUT
@Path("{accountId}")
@Consumes("application/xml")
@PreAuthorize("hasRole('AUTHORISED')")
public Response methodName
于 2014-06-12T10:42:56.060 に答える