关于spring Security的一些简单用法

简介
springSecurity是针对spring项目的安全框架,也是springBoot底层安全模块的技术选型,他可以实现强大的web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理.

首先我们需要记住这几个类:

  • WebSecurityConfigurationAdapter:自定义security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity:开启WebSecurity模式, @Enablexxxx就是开启某个功能
    Spring Security的两个主要的目标是"认证"和’‘授权’’(访问控制)
  • 认证(Authentication)
  • 授权(Authorization)
    这两个概念是通用的,不仅仅只在springsecurity中存在
package com.qiu.config;

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;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

这是一个固定的架子

添加权限代码:

http.authorizeRequests().antMatchers("/")
                .permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

这样就实现了有等级的人就可以访问相应的地址
如以下代码:

package com.qiu.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;
import org.springframework.security.crypto.password.PasswordEncoder;

@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");

        //没有权限会默认到登录页面,需要开启登录的页面
        http.formLogin();
    }
//认证,springboot 2.1.x可以直接使用
    //密码验证:PasswordEncoder
//在springSecurity 5.0+ 新增了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //这些数据正常来说应该是从数据库中读取

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("qiuzhikang").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");
    }
}

如果说需要连接数据库的话,从官方文档中可以获取到这么一段代码

在这里插入图片描述在这里插入图片描述附上完整代码:

package com.qiu.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;
import org.springframework.security.crypto.password.PasswordEncoder;

@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");

        //没有权限会默认到登录页面,需要开启登录的页面
        http.formLogin().loginPage("/toLogin");

        //注销.开启了注销功能,跳回首页
        //防止网站攻击:get不安全,明文传输,post可以,但是需要表单,所以security自己自带了一个
        http.csrf().disable();//关闭csrf功能,登出失败可能的原因就是这个
        http.logout().logoutSuccessUrl("/");
        //开启记住我功能,cookie的实现,默认保存两周
        http.rememberMe().rememberMeParameter("remember");//自定义接收前段的参数
    }
//认证,springboot 2.1.x可以直接使用
    //密码验证:PasswordEncoder
//在springSecurity 5.0+ 新增了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //这些数据正常来说应该是从数据库中读取

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("qiuzhikang").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");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章