Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将spring安全身份验证规则与数据库用户关联_Spring_Spring Security - Fatal编程技术网

如何将spring安全身份验证规则与数据库用户关联

如何将spring安全身份验证规则与数据库用户关联,spring,spring-security,Spring,Spring Security,我从spring in action中了解到了spring安全性,我想实现这一点:我有一个web应用程序,其中有两种用户存储在数据库中: <authentication-manager> <authentication-provider user-service-ref="userDetailsService" /> </authentication-manager> 1) 管理员 <authentication-manager>

我从spring in action中了解到了spring安全性,我想实现这一点:我有一个web应用程序,其中有两种用户存储在数据库中:

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
1) 管理员

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
2) 客户

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
这是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>
<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>

那么如何联系:

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>

到数据库用户条目(管理员和客户端)? 我将此部分添加到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>
<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

您不能这样映射

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
为此,您需要使用自定义项。使用userDetailsService,您可以从数据库加载用户并将其传递给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;
    }
}
<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
security.xml

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>