一、原有的配置文件中,增加注解@EnableGlobalMethodSecurity(prePostEnabled = true)

二、原有配置文件中,内存新建账号的时候添加角色

package Eleven.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.method.configuration.EnableGlobalMethodSecurity;
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; @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("123456")).roles("admin");
auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("123456")).roles("normal");
}
}

三、controller里面不同路径授予不同角色访问

package Eleven.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class AuthenticationTestController { @GetMapping("/user")
@PreAuthorize("hasAnyRole('normal')")
public String helloWorld(){
return "This is a user page!";
} @GetMapping("/admin")
@PreAuthorize("hasAnyRole('admin')")
public String getAdminInfo(){
return "This is Admin page!";
} }

Spring security 知识笔记【内存角色授权】的更多相关文章

  1. Spring security 知识笔记【自定义登录页面】

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. Spring security 知识笔记【入门】

    一.生成spring boot项目文件 二.pom文件如下 <?xml version="1.0" encoding="UTF-8"?> <p ...

  3. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  4. Spring Security OAuth笔记

    因为工作需要,系统权限安全方面可能要用到Spring Security OAuth2.0,所以,近几天了解了一下OAuth相关的东西.目前好像还没有系统的学习资料,学习主要是通过博客,内容都是大同小异 ...

  5. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  6. Spring Security中实现微信网页授权

    微信公众号提供了微信支付.微信优惠券.微信H5红包.微信红包封面等等促销工具来帮助我们的应用拉新保活.但是这些福利要想正确地发放到用户的手里就必须拿到用户特定的(微信应用)微信标识openid甚至是用 ...

  7. Spring Security学习笔记

    Spring Web Security是Java web开发领域的一个认证(Authentication)/授权(Authorisation)框架,基于Servlet技术,更确切的说是基于Servle ...

  8. SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置

    官方文档参考,5.1.2 中文参考文档,4.1 中文参考文档,4.1 官方文档中文翻译与源码解读 SpringSecurity 核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) ...

  9. Spring Security OAuth2 Demo —— 隐式授权模式(Implicit)

    本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_impilit_pattern.html 写在前面 在文章OAuth 2.0 概念及授权流程梳 ...

随机推荐

  1. Java-100天知识进阶-GC算法-知识铺(五)

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. GC算法 1.标记清除算法 优缺点:不需要额外空间,但是遍历空间花费大,而且会产生大量 ...

  2. redis之通信协议

    Redis 协议将传输的结构数据分为 5 种最小单元类型,单元结束时统一加上回车换行符号\r\n. 1.单行字符串 以 + 符号开头. 2.多行字符串 以 $ 符号开头,后跟字符串长度. 3.整数值 ...

  3. java jdb

    https://stackoverflow.com/questions/8155253/how-do-i-compile-in-debug-mode-netbeans-java-maven <p ...

  4. Python自定义注解

    Python3.0之后加入新特性Decorators,以@为标记修饰function和class.有点类似c++的宏和java的注解.Decorators用以修饰约束function和class,分为 ...

  5. Kubernetes PV与PVC的关系

    Kubernetes PV与PVC的关系 PersistenVolume(PV):对存储资源创建和使用的抽象,使得存储作为集群中的资源管理,分为有静态与动态.PersistentVolumeClaim ...

  6. GAN简介

    GAN Generative Adversarial Networks 生成对抗网络.学习真实世界的真实数据的分布,用于创造以假乱真的数据.比如前段时间很火的应用deep fake.deep nude ...

  7. C#调用Activex中串口电子秤的数据,并将电子秤的数据显示到前端页面

    大二的一个项目需要用到Activex技术将读取到串口中的数据在后台获取到,并将串口的数据写入数据库,这个过程需要在后台使用C#调用Activex控件已经使用的方法,然后在前端通过JavaScript进 ...

  8. HTTPS免费证书

    HTTPS 证书 一: 利用 cerbot 本地服务器 申请免费可用的证书.缺点就是三个月需要手动换一次 官网: https://certbot.eff.org/lets-encrypt/ubuntu ...

  9. Android App自动化测试实战(基于Python)(三)

    1.Native App自动化测试及Appuim框架介绍 android平台提供了一个基于java语言的测试框架uiautomator,它一个测试的Java库,包含了创建UI测试的各种API和执行自动 ...

  10. Java实现QQ邮件发送

    首先我们需要两个jar包,点击下面即可下载这两个包: JavaMail mail.jar 1.4.5 JAF(版本 1.1.1) activation.jar 我们这里采用QQ邮箱发送邮件为例,代码如 ...