SpringSecurity(安全)

在web开发中安全第一!  过滤器、拦截器可以实现安全问题

安全是一个非功能性的需求。

如做网站时设计之初就得考虑安全问题,不然可能就会有漏洞,用户隐私泄露等问题。如果后期添加的话就得造成大量代码得修改,得不偿失。

市面上常用安全框架shiroSpringSecurity ,他们两个很像。除了名字不一样,类不一样,功能差不多都类似。

主要功能都是认证、授权:

    认证:如用户密码登录错误。

    授权:如用户VIP1、VIP2、VIP3权限一样  。

具体可以使用的地方有:功能权限、访问权限、菜单权限等。如果用拦截器、过滤器的话就得写大量原生代码,太繁琐冗余了。

引入thymeleaf的包:

<!--web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--thymeleaf模板-->
<dependency>
     <groupId>org.thymeleaf</groupId>
     <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency> 

 SpringSecurity官网链接:https://spring.io/projects/spring-security#learn

下载完在resources文件下的static导入css、js的静态资源,在templates下导入页面模板,然后创建一个名为RouterController的class文件。

@Controller
public class RouterController {

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "views/login";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") Integer id){
        return "views/level1/"+id;
    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") Integer id){
        return "views/level2/"+id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") Integer id){
        return "views/level3/"+id;
    }
}

简介

Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!

记住几个类:

●WebSecurityConfigurerAdapter: 自定义Security策略

●AuthenticationManagerBuilder: 自定义认证策略

●@EnableWebSecurity:开启WebSecurity模式     (以后开启某一个功能都是使用 “@Enablexxxx” 格式)

Spring Security的两个主要目标是“认证”和“授权”(访问控制)。

“认证" (Authentication)

“授权”(Authorization)

这个概念是通用的,而不是只在Spring Security中存在。

参考官网: https://spring.io/projects/spring-security, 查看我们自己项目中的版本,找到对应的帮助文档:https://docs.spring.io/spring-security/site/docs/5.2.0.RELEASE/reference/htmlsingle

 使用Security

先引入security的配置文件:

        <!--security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

 创建一个config和controller同级文件夹,在里面创建一个class文件SecurityConfig.

Security授权

    //链式编程
    //授权
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        //首页所有人可以访问,功能页面只能有相应权限的人才能访问。
        //请求授权的规则
        httpSecurity.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限的话默认跳转到 login 登录页面  .loginPage("/toLogin")这是定制登录页.loginProcessingUrl("/login")
        httpSecurity.formLogin().loginPage("/toLogin");
        //防止网站攻击:get post  关闭csrf功能
        httpSecurity.csrf().disable();
        //开启了注销功能,反回首页
        httpSecurity.logout().logoutSuccessUrl("/");
        //开启记住账号密码功能  存入cookie 默认保存两周
        httpSecurity.rememberMe().rememberMeParameter("/remember");

}
.permitAll()表示这个页面所有用户都可以进,.hasRole("vip1") 表示只有拥有“VIP1”权限的用户才能进;这样就可以进行简单的权限拦截了。

Security认证

  //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                .and()
                .withUser("ljj").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip1")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }

  

  

  

  

 

  

posted @ 2020-08-27 14:15  吃完肥皂吐泡泡  阅读(298)  评论(0)    收藏  举报