SptringSecurity 学习笔记(一)Security的模式

文章介绍了如何在SpringSecurity中配置基本的HTTPbasic认证和form表单认证方式。通过添加依赖、配置安全类,设置拦截规则和账户授权,展示了两种认证模式的实现。同时,提到了解决升级SpringSecurity后密码编码问题的方法,并给出了动态配置权限的示例。
摘要由CSDN通过智能技术生成

SptringSecurity 学习笔记(一)Security模式

第一种 :basic认证(早期) 过时了
是一种简单的HTTP认证
简单的说 就是访问接口时需要输入账号密码进行授权
在这里插入图片描述

添加依赖

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

配置类

package com.lfy.study.config;

import org.springframework.context.annotation.Bean;
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.password.NoOpPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    /**
     * 新增Security
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //新增账户  可以访问什么接口
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password("12345")
                .authorities("/");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置认证方式 1.token  2.form
        //拦截所有请求 并走httpbasic模式
        http.authorizeRequests()
                .antMatchers("/**")
                .fullyAuthenticated().and().httpBasic();
    }

    /**
     * 解决 there is no passwordEncoder mapper for thi id "null"
     * 原因  :升级Security5.0以上密码支持多种加密方式 这个方法可以恢复以前模式   还有一种方法就是创建Sercurity时候添加加密方式
     *
     * @return
     */
    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }


}

原理: 验证账号密码是否相同 如果相同则返回一个cookie给浏览器 如果清除缓存则需要重新输入账号密码

第二种 form 表单模式

  1. 将拦截模式变成了formLogin();
  2. 可通过 .antMatchers(“/**”).hasAnyAuthority(“filterName”)配置 拦截规则 filterName拦截规则名称
   @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置认证方式 1.token  2.form
        //拦截所有请求 并走form模式
        http.authorizeRequests()
                .antMatchers("/**").hasAnyAuthority("filterName").and().formLogin();

    }

3.添加账户 配置加上 账户授权可以访问哪些接口

    /**
     * 新增Security
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //新增账户  可以访问什么接口
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password("12345")
                .authorities("filterName");//fiterName 是接口的
    }

完整代码

package com.lfy.study.config;

import org.springframework.context.annotation.Bean;
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.password.NoOpPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    /**
     * 新增Security
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置拦截规则

        //新增账户  可以访问什么接口
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password("12345")
                .authorities("filterName");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置认证方式 1.token  2.form
        //拦截所有请求 并走httpbasic模式
        http.authorizeRequests()
                .antMatchers("/**").hasAnyAuthority("admin").and().formLogin();

    }

    /**
     * 解决 there is no passwordEncoder mapper for thi id "null"
     * 原因  :升级Security5.0以上密码支持多种加密方式 这个方法可以恢复以前模式   还有一种方法就是创建Sercurity时候添加加密方式
     *
     * @return
     */
    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }


}

重点是将这种形式转换为动态形式才行


.permitAll()配置不需要校验权限

    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/**").hasAnyAuthority("admin")
                .antMatchers("/login").permitAll()//配置login接口不需要校验权限
                .and().formLogin()
        .loginPage("/login").and().csrf().disable()//用自己的登录页面 不用默认的登录页面;

    }

3.token后续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值