Spring Security学习笔记(一)
作者:mySoul 发布时间:2023-09-10 06:25:09
标签:Spring,Security,笔记,Java
介绍
这里学习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请求进行登录验证。
来源:https://www.iming.info/2020/06/15/%e5%ad%a6%e4%b9%a0%e5%ad%a6%e4%b9%a0springsecurity/


猜你喜欢
- 线程池模型一般的池化模型会有两个方法,用于获取资源和释放资源,就像这样:public interface XXPool{ &n
- 这篇文章主要介绍了springboot项目访问静态资源的配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 概述异步这个概念在不同语境下有不同的解释,比如在一个单核CPU里开启两个线程执行两个函数,通常认为这种调用是异步的,但对于CPU来说它是单核
- 注意:导包的时候API 11之前: android.text.ClipboardManagerAPI 11之后: android.conte
- 问题描述通过FeignClient调用微服务提供的分页对象IPage报错{"message": "Type d
- jwt简介冒泡排序:(Bubble Sort)是一种简单的交换排序。之所以叫做冒泡排序,因为我们可以把每个元素当成一个小气泡,根据气泡大小,
- Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框
- 本文实例为大家分享了Unity实现每日签到系统的具体代码,供大家参考,具体内容如下代码:using System;using System.
- Class类中获取方法:public Method[] getMethods();//获取包括自身和继承(实现)过来的所有的public方法
- 主要功能设计:用户、区域、物质类型、物质详情、物质申请和审核以及我的申请和通知公告以及灵活控制菜单权限主要技术实现:spring、 spri
- 本文实例讲述了 Android 7.0开发获取存储设备信息的方法。分享给大家供大家参考,具体如下:Android 7.0开发相较之前有不少改
- Kotlin的对象表达式与Java中的匿名内部类的主要区别:匿名内部类只能指定一个父类型,但对象表达式可以指定0~N个肤类型。一、对象表达式
- 一 技术发展技术的创新和发展都是为了解决一类问题二 框架设计Spring Framework 6大模块三 Spring AOP详解循环依赖问
- 这篇文档主要关注下配置修改后对应的 Java 对象是如何更新,并不关注整体的配置改动流程所有代码都来自 apollo-client 项目更新
- 本文所述为一个Android上传文件的源代码,每一步实现过程都备有详尽的注释,思路比较清楚,学习了本例所述上传文件代码之后,你可以应对其它格
- 前言在Mac中用android studio 导出jar包最重要的是需要配置gradle ,它包的导出也是通过gradle命令进行的。所以,
- 问题:在web里面用iframe连接一个html文件 - html文件里面是超链接 -&nb
- 前言我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印。关于文本水印,之前那篇教程里主要指的是单行字体
- //param objArr the expanded object of Array. &
- 这篇文章主要介绍了Java解析json报文实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可