java安全框架——Spring Security安全框架

spring Security是一个能够为基于spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在spring应用上下文中配置的bean,充分利用了springioc(控制反转),di(依赖注入)和aop(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制 编写大量重复代码的工作。

入门小demo:

pom.xml:


		4.2.4.RELEASE
	
	
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-test
			${spring.version}
		
		
			org.springframework
			spring-jdbc
			${spring.version}
		
		
			org.springframework.security
			spring-security-web
			4.1.0.RELEASE
		
		
			org.springframework.security
			spring-security-config
			4.1.0.RELEASE
		
		
			javax.servlet
			servlet-api
			2.5
			provided
		
	
	
	  		
	      
		  
				org.apache.maven.plugins
				maven-compiler-plugin
				3.2
				
					1.7
					1.7
					UTF-8
				
		        
	      
				org.apache.tomcat.maven
				tomcat7-maven-plugin
				
					
					8083
					
					/
				
	  	  
	     
    

web.xml:


		
  	 
		contextConfigLocation
		classpath:spring-security.xml
	 
	 
		
			org.springframework.web.context.ContextLoaderListener
		
	 	
	   
		springSecurityFilterChain  		 org.springframework.web.filter.DelegatingFilterProxy  
	   
	   
		springSecurityFilterChain  
		/*  
	 	

创建spring-security.xml配置文件:




	
	
		
			
	

	
	
		
			
				
					
			
	

在web.xml中配置了filter拦截器,安全框架拦截所有请求,拦截器需要有配置文件,配置文件是需要监听器来加载的,

spring-security.xml配置文件里面就是拦截的配置。

配置不被拦截的页面:

需要控制安全的放在标签中:


         :pattern="/**" 拦截所有,表示有ROLE_USER权限可以访问
                     login-page="/login.html"  login-processing-url="/login"/> : login-page表示自己的登录页, login-processing-url登录页处理的请求,在自己书写的登录页面中form表单的action路径必须也要是/login。登录成功跳转到/index.html页面,可以配置登录失败返回登录页面重新
          
        
        
    

认证器的任务就是判断用户名,密码好角色是否正确,如果正确认证成功。实际这些信息时存放在表里,这里只是做一个小demo,先暂时写死。

 

实际应用:(我们给系统中添加)

1.首先将上方的web.xml中的内容,放到项目中的web.xml中。

2.将spring-security.xml配置文件放到项目中(注意加载路径正确)。

3.修改spring-security.xml配置文件内容:

01.项目中spring-security.xml配置文件:



	
	
	
	
	
	
	
	
	
	
		
		
		
		
		
		
		
		
		
		
		
			
		
		
		
		
	
	
	
	
		
		
			
		
	
		

		
	
	
	
	
	
	
		
	


	

在SellerService中写接口:

public Seller findSellerBySellerIdAndStatus(String sellerId, String status);

UserDetailServiceImpl类:

public class UserDetailServiceImpl implements UserDetailsService {

	// 根据用户名查询tb_seller 表
	private SellerService sellerService;

	public void setSellerService(SellerService sellerService) {
		this.sellerService = sellerService;
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		// 认证
		Seller seller = sellerService.findSellerBySellerIdAndStatus(username, "1");
		if (null != seller) {
			Set authorities = new HashSet<>();
			authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
			return new User(seller.getSellerId(), seller.getPassword(), authorities);

		} else {
			return null;
		}
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Java框架)