Note_55 SpringSecurity(一) —— Hello Security

1.依赖
	
		org.springframework.security
		spring-security-web
		5.0.1.RELEASE
	

	
		org.springframework.security
		spring-security-config
		5.0.1.RELEASE
	


======================================================
2.spring-security.xml



    
    
    
    

    
    
        
        


        
        

        
        


        
        
    


    
    
        
        
                




        
    



======================================================
3.web.xml
	
    
        contextConfigLocation
        classpath:spring-security.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        springSecurityFilterChain
        org.springframework.web.filter.DelegatingFilterProxy
    
    
        springSecurityFilterChain
        /*
    
	
======================================================
4.service
	//继承 UserDetailService 接口
	public interface UserService extends UserDetailsService { }
	
	//实现 UserService 接口
	Service("userService")
	public class UserServiceImpl implements UserService {

		@Autowired
		private UserDao userDao;	//注入 UserDao

		public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
			//调用 dao 查询数据库中的用户信息
			UserInfo userInfo = userDao.findByName(username);

			//将角色加入到 List 集合【此处角色应从数据库查出】
			List list = new ArrayList();
			SimpleGrantedAuthority role = new SimpleGrantedAuthority("ROLE_USER");
			list.add(role);

			/*
				创建 spring security 提供的 User 对象,传入 查询出的 username 、password
				自动与 session 中的 username 、password 进行比对,正确则通过验证
				此处 session 中的数据由 spring security 封装
				明文密码需要拼接 {noop} ,否则将以密文比对
			*/
                    User user = new User(userInfo.getUsername(),"{noop}" + userInfo.getPassword(),
                            userInfo.getStatus() == 0 ? false : true,true,true,true,list);


			return user;
		}
	}

 

你可能感兴趣的:(Java,EE)