Spring4和SpringSecurity4的整合(一)

SpringSecurity的官方文档及其简单,他的示例配置就是在xml文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程
首先pom导入jar包:
pom.xml


        
            org.glassfish.web
            javax.servlet.jsp.jstl
            1.2.2
        
        
            javax.servlet
            jstl
            1.2
        
        
            javax
            javaee-api
            7.0
            provided
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            junit
            junit
            3.8.1
            test
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        

        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework.security
            spring-security-web
            4.1.3.RELEASE
        
        
            org.springframework.security
            spring-security-config
            4.1.3.RELEASE
        
    

SpringSecurity.xml配置


    
        
    
    
    
    
        
        
    

UserDetailService实现类

public class MyUserDetailService implements UserDetailsService  {

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
//在这里为了方便测试直接固定了
        MyUser myUser = new MyUser();
        System.out.println(username + "load的值");
        myUser.setUser_name("a");
        myUser.setUser_password("aa");
        myUser.setUser_role("ROLE_ADMIN");
        return new MyUserDetail(myUser, getAuthorities());
    }

    private Collection getAuthorities() {
        Collection grantedAuthorities = new ArrayList<>();
        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }

}

MyUserDetail实现类:为了以后能更多的对用户进行操作

public class MyUserDetail implements UserDetails {
    private MyUser myUser;
    private Collection authorities;

    public MyUserDetail(MyUser user,Collection authorities) {
        this.myUser = user;
        this.authorities = authorities;
    }

    @Override
    public Collection getAuthorities() {
        // TODO Auto-generated method stub
        return authorities;
    }

    @Override
    public String getPassword() {
        return myUser.getUser_password();
    }

    @Override
    public String getUsername() {
        return myUser.getUser_name();
    }

    @Override
    public boolean isAccountNonExpired() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

AuthenticationProvider类

public class SecurityProvider implements AuthenticationProvider {
    @Autowired
    private MyUserDetailService userDetailsService;
    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        UserDetails userDetails = userDetailsService.loadUserByUsername("a");
        if (userDetails == null) {
            throw new UsernameNotFoundException("账号不存在");
        }
        return new UsernamePasswordAuthenticationToken(userDetails, "aa",
                userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class authentication) {
        // TODO Auto-generated method stub
        return UsernamePasswordAuthenticationToken.class.equals(authentication);
    }

其他的正常配置即可
地址:https://github.com/Somersames...

你可能感兴趣的:(springjava)