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");
        }
    
    
    
    }
  • 相关阅读:
    2018-04-27 搭建Python官方文档翻译环境-汉化示例代码
    2018-04-21 搭建Python官方文档翻译环境
    2018-01-19 Xtext试用: 5步实现一个(中文)JVM语言
    2018-02-27 "Literate Programming"一书摘记之一
    2018-02-18 Antlr4实现简单语言之条件语句
    2018-02-17 中文代码示例[译]Scala中创建隐式函数
    2018-02-16 中文代码示例之冒泡算法, 后感
    C code example for strdup
    Hash table: why size should be prime?
    Hash table lengths and prime numbers
  • 原文地址:https://www.cnblogs.com/xie-qi/p/14618793.html
Copyright © 2011-2022 走看看