1

私は実行中のSpringからSpring Securityについて読みました。これを実装したいと思います:データベースに2種類のユーザーが保存されているWebアプリケーションがあります:

1)管理者

2)クライアント

これは spring-security.xml です。

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

<http auto-config="true">
    <intercept-url pattern="/welcome*" access="ROLE_USER" />
</http>

<authentication-manager>
  <authentication-provider>
    <user-service>
    <user name="name" password="password" authorities="ROLE_USER" />
    </user-service>
  </authentication-provider>
</authentication-manager>

 </beans:beans>

関連付ける方法:

<user-service> <user name="name" password="password" authorities="ROLE_USER" /> </user-service> データベースのユーザー エントリ (管理者とクライアント) に ? このセクションを web.xml に追加しました:

 <!-- Spring Security -->
<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>
4

1 に答える 1

2

このようにマッピングすることはできません。

この目的のために、カスタムUserDetailsS​​erviceを使用する必要があります。userDetailsS​​ervice を使用して、データベースからユーザーをロードし、Spring セキュリティ フレームワークに渡すことができます。

public class UserDetailsServiceImpl implements UserDetailsService {
    public UserDetails loadUserByUsername(String userName)
            throws UsernameNotFoundException, DataAccessException {
        User user = getUser(userName); //Load user from database
        if (user == null) {
            throw new UsernameNotFoundException("User not found: " + userName);
        }
        return user;
    }
}

security.xml

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
于 2013-02-21T03:19:43.927 に答える