spring security 学习

转载文章,学习

 

Springside 3.3.1中权限控制用的是用户-角色-授权的方式。相应的建立了三张表
acct_authority     权限表
acct_role         角色表
acct_role_authority 角色权限关系表
acct_user          用户表
acct_user_role      用户角色关系表

spring security 设计了一个<http>标签用来配置url与 授权 的关系
applicationContext-security.xml:

Java代码 复制代码
  1. <description>SpringSecurity安全配置</description>   
  2.     <!-- http安全配置 -->   
  3.     <!-- auto-config属性其实是<form-login/>,<http-basic/>,<logout/>的缩写.   
  4.          没配置登录页面的情况下,spring自动生成一个. -->   
  5.     <s:http auto-config= "true"  use-expressions= "true" >   
  6.         <s:intercept-url pattern= "/css/**"  filters= "none"  />   
  7.         <s:intercept-url pattern= "/img/**"  filters= "none"  />   
  8.         <s:intercept-url pattern= "/js/**"  filters= "none"  />   
  9.     <!-- 设置通过这个url需要什么样的权限  -->   
  10.         <s:intercept-url pattern= "/account/user!save*"  access= "hasAnyRole('ROLE_修改用户')"  />   
  11.         <s:intercept-url pattern= "/account/user!delete*"  access= "hasAnyRole('ROLE_修改用户')"  />   
  12.         <s:intercept-url pattern= "/account/user*"  access= "hasAnyRole('ROLE_浏览用户')"  />   
  13.         <s:intercept-url pattern= "/account/role!save*"  access= "hasAnyRole('ROLE_修改角色')"  />   
  14.         <s:intercept-url pattern= "/account/role!delete*"  access= "hasAnyRole('ROLE_修改角色')"  />   
  15.         <s:intercept-url pattern= "/account/role*"  access= "hasAnyRole('ROLE_浏览角色')"  />   
  16.     <!-- < s:intercept-url pattern= '/login.htm*'  filters= 'none' />这个标签是把验证排除在外。因为这是一个登录页面。   
  17.         而s:form-login标签的login-page则指定遇到受限资源需要进行登录时候的页面.   
  18.          default -target-url指定了从登录页面登录后进行跳转的页面   
  19.         always-use- default -target这个属性表示登录成功后强制跳转到 default -target-url这个地址。默认情况下如果是从首先页面过来登录的,那么跳转是跳转回受限页面。 -->   
  20.         <s:form-login login-page= "/login.action"   default -target-url= "/account/role.action"  authentication-failure-url= "/login.action?error=true"   always-use- default -target = "ture" />   
  21.         <s:logout logout-success-url= "/"  />   
  22.     </s:http>  
<description>SpringSecurity安全配置</description>
	<!-- http安全配置 -->
	<!-- auto-config属性其实是<form-login/>,<http-basic/>,<logout/>的缩写.
		 没配置登录页面的情况下,spring自动生成一个. -->
	<s:http auto-config="true" use-expressions="true">
		<s:intercept-url pattern="/css/**" filters="none" />
		<s:intercept-url pattern="/img/**" filters="none" />
		<s:intercept-url pattern="/js/**" filters="none" />
	<!-- 设置通过这个url需要什么样的权限  -->
	    <s:intercept-url pattern="/account/user!save*" access="hasAnyRole('ROLE_修改用户')" />
		<s:intercept-url pattern="/account/user!delete*" access="hasAnyRole('ROLE_修改用户')" />
		<s:intercept-url pattern="/account/user*" access="hasAnyRole('ROLE_浏览用户')" />
	    <s:intercept-url pattern="/account/role!save*" access="hasAnyRole('ROLE_修改角色')" />
		<s:intercept-url pattern="/account/role!delete*" access="hasAnyRole('ROLE_修改角色')" />
		<s:intercept-url pattern="/account/role*" access="hasAnyRole('ROLE_浏览角色')" />
	<!-- < s:intercept-url pattern='/login.htm*' filters='none'/>这个标签是把验证排除在外。因为这是一个登录页面。
		而s:form-login标签的login-page则指定遇到受限资源需要进行登录时候的页面.
		default-target-url指定了从登录页面登录后进行跳转的页面
		always-use-default-target这个属性表示登录成功后强制跳转到default-target-url这个地址。默认情况下如果是从首先页面过来登录的,那么跳转是跳转回受限页面。 -->
		<s:form-login login-page="/login.action" default-target-url="/account/role.action" authentication-failure-url="/login.action?error=true"  always-use-default-target ="ture"/>
		<s:logout logout-success-url="/" />
	</s:http>


要实现数据库与spring security的结合,我们必需实现UserDetailsService接口中的loadUserByUsername(String username)方法,此方法返回一个org.springframework.security.core.userdetails.User对象.因此,怎么样设计数据库都可以,不管我们是用一个表还是两个表或者说是三个表都可以,也不管我们是用户-授权,还是用户-角色-授权,或者是用户-用户组-角色-授权,这些spring security都不关心,它只关心返回的那个User对角.至于怎么样去查询数据库那就是我们的事了.
Springside 3.3.1 mini-web 中的UserDetailsServiceImpl类:

Java代码 复制代码
  1. /**  
  2.  * 实现SpringSecurity的UserDetailsService接口,实现获取用户Detail信息的回调函数.  
  3.  *   
  4.  * @author calvin  
  5.  */   
  6. @Transactional (readOnly =  true )   
  7. public   class  UserDetailsServiceImpl  implements  UserDetailsService {   
  8.   
  9.      private  AccountManager accountManager;   
  10.      /**  
  11.      * 获取用户Details信息的回调函数.  
  12.      */   
  13.      public  UserDetails loadUserByUsername(String username)  throws  UsernameNotFoundException, DataAccessException {   
  14.   
  15.         User user = accountManager.findUserByLoginName(username);   
  16.          if  (user ==  null ) {   
  17.              throw   new  UsernameNotFoundException( "用户"  + username +  " 不存在" );   
  18.         }   
  19.   
  20.         Set<GrantedAuthority> grantedAuths = obtainGrantedAuthorities(user);   
  21.            
  22. //      System.out.println(grantedAuths);   
  23.   
  24.          //-- mini-web示例中无以下属性, 暂时全部设为true. --//   
  25.          boolean  enabled =  true ;   
  26.          boolean  accountNonExpired =  true ;   
  27.          boolean  credentialsNonExpired =  true ;   
  28.          boolean  accountNonLocked =  true ;   
  29.   
  30.         UserDetails userdetails =  new  org.springframework.security.core.userdetails.User(user.getLoginName(), user   
  31.                 .getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);   
  32.            
  33.          return  userdetails;   
  34.     }   
  35.      /**  
  36.      * 获得用户所有角色的权限集合.  
  37.      */   
  38.      private  Set<GrantedAuthority> obtainGrantedAuthorities(User user) {   
  39.         Set<GrantedAuthority> authSet = Sets.newHashSet();   
  40.          for  (Role role : user.getRoleList()) {   
  41.              for  (Authority authority : role.getAuthorityList()) {   
  42.                 authSet.add( new  GrantedAuthorityImpl(authority.getPrefixedName()));   
  43.             }   
  44.         }   
  45.          return  authSet;   
  46.     }   
  47.      @Autowired   
  48.      public   void  setAccountManager(AccountManager accountManager) {   
  49.          this .accountManager = accountManager;   
  50.     }   
  51. }  
/**
 * 实现SpringSecurity的UserDetailsService接口,实现获取用户Detail信息的回调函数.
 * 
 * @author calvin
 */
@Transactional(readOnly = true)
public class UserDetailsServiceImpl implements UserDetailsService {

	private AccountManager accountManager;
	/**
	 * 获取用户Details信息的回调函数.
	 */
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {

		User user = accountManager.findUserByLoginName(username);
		if (user == null) {
			throw new UsernameNotFoundException("用户" + username + " 不存在");
		}

		Set<GrantedAuthority> grantedAuths = obtainGrantedAuthorities(user);
		
//		System.out.println(grantedAuths);

		//-- mini-web示例中无以下属性, 暂时全部设为true. --//
		boolean enabled = true;
		boolean accountNonExpired = true;
		boolean credentialsNonExpired = true;
		boolean accountNonLocked = true;

		UserDetails userdetails = new org.springframework.security.core.userdetails.User(user.getLoginName(), user
				.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);
		
		return userdetails;
	}
	/**
	 * 获得用户所有角色的权限集合.
	 */
	private Set<GrantedAuthority> obtainGrantedAuthorities(User user) {
		Set<GrantedAuthority> authSet = Sets.newHashSet();
		for (Role role : user.getRoleList()) {
			for (Authority authority : role.getAuthorityList()) {
				authSet.add(new GrantedAuthorityImpl(authority.getPrefixedName()));
			}
		}
		return authSet;
	}
	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
}


最后在applicationContext-security.xml配置保用userDetailsService类提供的用户信息认证

Java代码 复制代码
  1. <!-- 认证配置, 使用userDetailsService提供的用户信息 -->   
  2. <s:authentication-manager alias= "authenticationManager" >   
  3.         <s:authentication-provider user-service-ref= "userDetailsService" >   
  4.             <s:password-encoder hash= "plaintext"  />   
  5.         </s:authentication-provider>   
  6.     </s:authentication-manager>   
  7.   
  8.     <!-- 项目实现的用户查询服务 -->   
  9.     <bean id= "userDetailsService"   class = "org.springside.examples.miniweb.service.account.UserDetailsServiceImpl"  />   
  10. </beans>  
<!-- 认证配置, 使用userDetailsService提供的用户信息 -->
<s:authentication-manager alias="authenticationManager">
		<s:authentication-provider user-service-ref="userDetailsService">
			<s:password-encoder hash="plaintext" />
		</s:authentication-provider>
	</s:authentication-manager>

	<!-- 项目实现的用户查询服务 -->
	<bean id="userDetailsService" class="org.springside.examples.miniweb.service.account.UserDetailsServiceImpl" />
</beans>


以上实现了对url的仅限访问.
如果对部方url中的具体操作作认证,则可以在页面使用标签:

Java代码 复制代码
  1. <security:authorize ifAnyGranted= "ROLE_浏览角色" >   
  2.     <security:authorize ifNotGranted= "ROLE_修改角色" >   
  3.         <a href= "role!input.action?id=${id}" >查看</a>   
  4.     </security:authorize>   
  5. </security:authorize>  
<security:authorize ifAnyGranted="ROLE_浏览角色">
	<security:authorize ifNotGranted="ROLE_修改角色">
		<a href="role!input.action?id=${id}">查看</a>
	</security:authorize>
</security:authorize>


在web.xml中配置spring security 的上下文

Java代码 复制代码
  1. <!-- SpringSecurity filter-->   
  2.     <filter>   
  3.         <filter-name>springSecurityFilterChain</filter-name>   
  4.     <filter- class >org.springframework.web.filter.DelegatingFilterProxy</filter- class >   
  5.     </filter>   
  6.   
  7. <!-- Filter 映射 -->   
  8.     <filter-mapping>   
  9.         <filter-name>springSecurityFilterChain</filter-name>   
  10.         <url-pattern>/*</url-pattern>   
  11.     </filter-mapping>  
<!-- SpringSecurity filter-->
	<filter>
		<filter-name>springSecurityFilterChain</filter-name>
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>

<!-- Filter 映射 -->
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


程序跳转流程:
用户输入用户名和密码,程序通过web.xml中配置的springsecurity上下文到applicationContext-security.xml中找到认证处理类   userdetailsService类,
在 loadUserByUsername(String username)方法,程序去查找到用户,再通过用户找到它所属的角色,再通过角色找到它所拥有的权限.再将这些信息封装成org.springframework.security.core.userdetails.User对象返回.如果在查找当中用户不存在, 程序会通过applicationContext-security.xml配置文件中的 <s:http>标签中的<s:form-login >中的authentication-failure-url 属性值返回给用户,如果验证通过则返回default-target-url属性值给用户.用户在使用程序时,程序会通过applicationContext-security.xml文件中将<s:http> 标签中的<s:intercept-url> 中url所需要的权限与用户的权限作对比,如果用户拥有此权限,则会直接进行跳转,如果没有权限,则返回<s:http>标签中的<s:form-login >中的authentication-failure-url 属性值.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值