springSecurity入门案例

1. pom.xml中设置

1.1 pom.xml设置打包方式为war

war

1.2引入依赖(Tomcat7插件可以不用)

	
        
            org.springframework.security
            spring-security-web
            5.0.5.RELEASE
        
        
            org.springframework.security
            spring-security-config
            5.0.5.RELEASE
        
        
            javax.servlet
            servlet-api
            2.5
        
    
    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                
                    8080
                    /
                
            
        
    

2.web.xml配置



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

此时spring-security.xml显红

3.resources下创建spring-security.xml文件



    
    
        
        
        
        
        
        
    
    
    
        
            
                
                
            
        
    

4.创建一个页面并启动tomcat

4.1创建页面 略

4.2启动tomcat

4.2.1访问网页 自动跳转到/login并输入

springSecurity入门案例_第1张图片

4.2.2有时会弹出404 无视重新输入创建的网页

springSecurity入门案例_第2张图片

登录成功 跳转到创建的页面

springSecurity入门案例_第3张图片

5 spring-security.xml重新配置

5.1由于springSecurity自动创建的登录页面比较差指定网页


        

由于之前配置的没登录默认拦截所有资源,须配置放行指定的资源文件

5.1.2 放行指定资源



5.1.3创建login.htm login_error.html success.html页面

login.html中action必须为/login 提交方式post name属性必须有username和password

用户名
密码

5.1.4启动tomcat

访问  localhost:8080/success.html

5.1.4.1此时会自动跳转到自定义的login页面

springSecurity入门案例_第4张图片

5.1.4.2登录 提示403

springSecurity入门案例_第5张图片

5.1.4.3

由于springsecurity创建的表单带有一个隐藏域证,而自定义的页面提交没有
springSecurity入门案例_第6张图片
此时需要关闭掉验证
srping-security.xml中添加csrf标签


    
        
        
        
        
        
        
        
        
    

此时登录 成功

6 使用bcrypt加密策略

6.1 下载将BCrypt.java文件移动到项目中

6.1.1 创建BcryptDemo测试

public class BcryptDemo {
    public static void main(String[] args) {
        //获取盐
        String gensalt = BCrypt.gensalt();
        System.out.println(gensalt);
        //使用盐加密123
        String pw = BCrypt.hashpw("123", gensalt);
        System.out.println(pw);
        //判断123是否与加密后的密码相等
        boolean checkpw = BCrypt.checkpw("123", pw);
        System.out.println(checkpw);
    }
}

运行
springSecurity入门案例_第7张图片

6.2 springSecurity配置

6.2.1加密方式一

将加密后的密码在user-service中使用


        
            
                
            
        
    

password大括号中常用的
1. {noop} 不使用加密策略
2. {bcrypt} 使用bcrypt加密策略
3. {md5} 使用md5加密策略

此时重新运行tomcat登录 成功

6.2.2加密方式二

6.2.2.1 创建一个java类实现UserDetailsService

com.demo.service.impl.UserDetailsServiceImpl

6.2.2.2 重写未实现的方法

public class UserDetailsServiceImpl implements UserDetailsService {
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List grantedAuthorityList = new ArrayList();
        // 添加输入的用户名s的权限    实际是从数据库中读取
        grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        // 不限制用户名  只要密码是123就让登陆
        return new User(s, "$2a$10$AS4KhUXju2vY0djQMNrTiO7mY9Td67wmKYGH27dL8MD0./K38u3D6", grantedAuthorityList);
    }
}

6.2.2.3 spring-security.xml配置


        
            
            
        
    
    
    

重启tomcat登录 成功!

最终spring-security.xml配置文件



    
    
    
    
    
        
        
        
        
        
        
        
        
    
    
    
        
            
            
        
    
    
    

你可能感兴趣的:(知识)