zoukankan      html  css  js  c++  java
  • Spring Security学习

    一)简介

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

    记住几个类:

    ~WebSecurityConfigureAdapter:自定义Security策略

    ~AuthenticationManagerBuilder:自定义认证策略

    ~@EnableWebSecurity:开启WebSecurity模式

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

    “认证”:(Authentication)

    “授权”:(Authorization)

    这个概念是用用的,而不是只在Spring Security找那个存在;

    二)环境搭建和整合;

    导入包:

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

    授权和认证:

    package com.example.demo.config;
    
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    
    //AOP 拦截器
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        //链式编程
        //授权
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //首页可以所有人访问,功能页只有对应有权限的人才能访问
            //请求授权的规则
            http.authorizeRequests()
                    .antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
    
            //没有权默认会到登录页面,需要开启登录的页面
            //login 定制登录页面 loginPage("/toLogin")
            http.formLogin().loginPage("/toLogin");
            //防止网站攻击  因为是get请求,明文传输 要变为post请求才行
            http.csrf().disable();//关闭csrf(请求攻击)功能  登录失败可能的原因之一
            //开启注销,默认跳转到首页
            http.logout().logoutSuccessUrl("/");
            //开启记住我功能 cookie:默认保存两周   自定义接收前端的参数
            http.rememberMe().rememberMeParameter("remember");
        }
    
        //认证 springboot 2.1.x 可以直接使用
        //密码编码:PassWordEncoder
        //在spring security 5.0+ 新增了很多的加密方法
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            //这些数据正常都是从数据库中读
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                    .and()
                    .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                    .and()
                    .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
        }
    
    
    
    }
  • 相关阅读:
    spring jdbcTemplate使用queryForList示例
    凡事预则立,不立则废:好计划是成功的开始——布利斯定理
    传世智库:初识IPD-集成产品开发
    骑手送外卖获奖1500多万元又被撤销 法律专家:不能一扣了之
    leetcode-----129. 求根到叶子节点数字之和
    leetcode-----128. 最长连续序列
    leetcode-----127. 单词接龙
    leetcode-----126. 单词接龙 II
    leetcode-----125. 验证回文串
    leetcode-----124. 二叉树中的最大路径和
  • 原文地址:https://www.cnblogs.com/xie-qi/p/14618793.html
Copyright © 2011-2022 走看看