Spring Security学习笔记(一)

 更新时间:2020年09月03日 09:23:28   作者:mySoul  
这篇文章主要介绍了Spring Security的相关资料,帮助大家开始学习Spring Security框架,感兴趣的朋友可以了解下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

介绍

这里学习SpringSecurity,对SpringSecurity进行学习。

基本用法

添加依赖

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

添加接口

package com.example.demo.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class Test {
  @RequestMapping("/test")
  public String test(){
    return "test";
  }
}

启动项目

可以看到日志中,已经有了密码

访问接口,此时已经有了登录页面

输入用户名和密码

用户名: user
密码 984cccf2-ba82-468e-a404-7d32123d0f9c

此时已经登录成功

配置用户名和密码

在配置文件中,进行配置

spring:
security:
user:
name: ming
password: 123456
roles: admin

输入用户名和密码,可以正常登录

基于内存的认证

需要自定义类继承 WebSecurityConfigurerAdapter
实现自定义的配置
这里基于内存的配置,如下

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
  PasswordEncoder passwordEncoder(){
    return NoOpPasswordEncoder.getInstance();
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("admin").password("123").roles("admin");
  }
}

这里基于内存的配置

HttpSecurity

这里对某些方法进行拦截

package com.ming.demo.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  //基于内存的用户存储
  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("itguang").password("123456").roles("USER").and()
        .withUser("admin").password("{noop}" + "123456").roles("ADMIN");
  }





  //请求拦截
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().permitAll()
        .and()
        .formLogin()
        .permitAll()
        .and()
        .logout()
        .permitAll();
  }


}

这里成功完成了post请求进行登录验证。

以上就是Spring Security学习笔记(一)的详细内容,更多关于Spring Security的资料请关注程序员之家其它相关文章!

相关文章

  • java计算两个日期之间相差天数的4种方法详解

    java计算两个日期之间相差天数的4种方法详解

    这篇文章主要给大家介绍了关于java计算两个日期之间相差天数的4种方法,本文简短地介绍java中多种方式求两个日期的差量,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • 在SpringBoot中注入RedisTemplate实例异常的解决方案

    在SpringBoot中注入RedisTemplate实例异常的解决方案

    这篇文章主要介绍了在SpringBoot中注入RedisTemplate实例异常的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • java web开发之实现购物车功能

    java web开发之实现购物车功能

    这篇文章主要为大家详细介绍了java web开发之实现购物车功能的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • JAVA使用POI获取Excel的列数与行数

    JAVA使用POI获取Excel的列数与行数

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 下面这篇文章给大家介绍了JAVA使用POI获取Excel列数和行数的方法,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-12-12
  • Java实现5种负载均衡算法(小结)

    Java实现5种负载均衡算法(小结)

    负载均衡是将客户端请求访问,通过提前约定好的规则转发给各个server,本文主要介绍了Java实现5种负载均衡算法,具有一定的参考价值,感兴趣的可以了解一下
    2022-06-06
  • Java中集合关系图及常见操作详解

    Java中集合关系图及常见操作详解

    这篇文章主要为大家详细介绍了Java中集合关系图及常见操作,解析Java中的集合类型的继承关系图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Mybatis useGeneratedKeys参数用法及问题小结

    Mybatis useGeneratedKeys参数用法及问题小结

    这篇文章主要介绍了Mybatis useGeneratedKeys参数用法及遇到的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • Java如何使用正则表达式从字符串中提取数字

    Java如何使用正则表达式从字符串中提取数字

    这篇文章主要介绍了Java如何使用正则表达式从字符串中提取数字问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java?面向对象通过new揭开对象实例化

    Java?面向对象通过new揭开对象实例化

    各位铁汁们大家好呀,我们上次博客讲了,通过?Student?student1?=?new?Student();就可以实例化一个对象,这个对象就有Student类中的所以成员变量。可是?对象student1?和?类Student到底是怎样建立联系的,在内存中到底发生了什么
    2022-04-04
  • IDEA快速部署Spring?Boot?项目到Docker的实现方法

    IDEA快速部署Spring?Boot?项目到Docker的实现方法

    本文主要介绍了IDEA快速部署Spring?Boot?项目到Docker的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07

最新评论

?


http://www.vxiaotou.com