Spring Security的简单使用

在Spring Security中如果想要使用数据进行认证操作,有很多种操作方式,这里我们介绍使用UserDetails、
UserDetailsService来完成操作。
创建service继承UserDetailsService

public interface UserService extends UserDetailsService

实现userService中的方法

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = this.userDao.findByUserName(username);
        User user = new User(userInfo.getUsername(), "{noop}" + userInfo.getPassword(), userInfo.getStatus() == 0? false:true,true,true,true, getAuthority(userInfo.getRoles()));
        return user;
    }
    private List getAuthority(List roles){
        List authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
        }
        return authorities;
    }
}

springSecurity.xml中的一些配置




    
    
    
    
    
    

    
    
        
        

        
        

        
        

        
        

    

    
    
        
            
        
    

    
    

    


web.xml中的配置


contextConfigLocation
classpath:spring-security.xml


org.springframework.web.context.ContextLoaderListener


springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy


springSecurityFilterChain
/*

你可能感兴趣的:(Spring Security的简单使用)