spring-security 1:配置

1:引入依赖包

    
      org.springframework.security
      spring-security-web
      ${springVersion}
    
    
      org.springframework.security
      spring-security-config
      ${springVersion}
    

如果写jsp下面的包就有用

    
      org.springframework.security
      spring-security-taglibs
      ${springVersion}
    

2:配置spring-security.xml





    
    
    
    



    
        
        

        
        


    
    
    
    
        
            
                
                
                
            
        

        
            
        
    

3:user-service-ref="accountService"里的bean实现

@Service("accountService")
public class AccountServiceImpl implements UserDetailsService {

    @Autowired
    @Qualifier("accountDao")
    private IAccountDao accountDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Account account = accountDao.fetchAccount1(s);
        if (account == null) {
            throw new UsernameNotFoundException("无帐户");
        }

        List l = new ArrayList<>();
        for (Role s1 : account.getAuthority()) {
            l.add(new SimpleGrantedAuthority(s1.getName()));
        }
        System.out.println(account);
        return new User(account.getUsername(),
                account.getPassword(),
                account.getStatus() == 1,
                true,
                true,
                true, l);
    }
}

4:spring-security什么时候加载

  
    contextConfigLocation
    classpath:applicationContext.xml,classpath:spring-security.xml
  

  
    org.springframework.web.context.ContextLoaderListener
  

或在applicationContext.xml



    
        
    

    

你可能感兴趣的:(spring-security 1:配置)