日期:2014-05-16  浏览次数:20425 次

spring security2【与数据库权限关联】
  1. 设计数据库

??????????????数据库权限概念?????????

???????????? 角色用户组权限关联

????????????

???????????? 数据库实体图:

???????????

???????????

?

??????2、将用户权限表与springsecurity相关联

?

?????????????????????使用自定义的provider

???????????????????

	<security:authentication-manager alias="authenticationManager">
		<security:authentication-provider
			ref="multipleAuthenticationProvider">
			<!-- <security:user-service> <security:user name="admin" password="admin" 
				authorities="ROLE_USER"/> <security:user name="manager" password="manager" 
				authorities="ROLE_USER"/> </security:user-service> -->                           <!-- 内存用户测试 -->
			<!-- <security:jdbc-user-service data-source-ref=""/> --> <!-- 数据库源测试 -->
			<!-- <security:ldap-user-service/> -->                    <!-- ladp数据源测试 -->
			<!-- <security:password-encoder> -->                      <!-- 密码encoder -->
			<!-- </security:password-encoder> -->
		</security:authentication-provider>

	</security:authentication-manager>

?

	<!-- 配置身份验证器 定义登陆验证过滤器 过后调用 -->
	<bean id="multipleAuthenticationProvider"
		class="com.bbs.security.authentication.provider.MultipleAuthenticationProvider">
		<property name="authenticationProviders">
			<list>
				<ref bean="forendAuthenticationProvider" />
			</list>
		</property>
	</bean>


?

	<!-- 前台验证器并构建新用户凭证 定义登陆验证过滤器 过后调用 并调用userDetailsService 通过用户名将用户和用户的角色装配 
		数据库中验证用户 重新构建UsernamePasswordAuthenticationToken传递给决策管理器进行授权管理 -->
	<bean id="forendAuthenticationProvider"
		class="com.bbs.security.authentication.provider.ForendAuthenticationProvider">
		<property name="userDetailsService" ref="forendUserDetailsService"></property>
		<property name="passwordEncoder" ref="shaPasswordEncoder"></property>
		<property name="saltSource" ref="saltSource"></property>
	</bean>

?

	<!-- 配置加密策略 -->
	<bean id="shaPasswordEncoder"
		class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
		<!-- 加密方式 SHA-256 -->
		<constructor-arg value="256" />
	</bean>

?

	<!-- 配置密码的盐值 -->
	<bean id="saltSource"
		class="org.springframework.security.authentication.dao.ReflectionSaltSource">
		<!-- 以用户名作为加密盐值 -->
		<property name="userPropertyToUse" value="username"></property>
	</bean>

?

	<bean id="forendUserDetailsService"
		class="com.bbs.security.authentication.userdetailservice.ForendUserDetailsService"></bean>


运行顺序是解释xml,提供权限验证,manager提供管理权限验证,加载bean,配置bean。

自定义的multipleAuthenticationProvider

import java.util.List;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;