首页 > 其他分享 >springsecurity

springsecurity

时间:2023-09-07 16:56:57浏览次数:34  
标签:123456 http BCryptPasswordEncoder antMatchers springsecurity new password

编辑 pom.xml,添加 spring-boot-starter-securtiy 依赖即可。添加后项目中所有的资源都会被保护起来。

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

编写SecurityConfig

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,功能页只有对应有权限的人才能访问
        http.authorizeHttpRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //没有权限会默认到登录页,需要开启登录的页面
        // /login
        http.formLogin();

        //防止网站攻击: get,post
        http.csrf().disable();//关闭csrf功能,登出失败可能存在的原因


        //注销,开启了注销功能,跳到首页
        http.logout().logoutSuccessUrl("/");
    }

    //认证
    //密码编码:PasswordEncoder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常应该从数据库中读
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("lzj").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");

    }
}

  

标签:123456,http,BCryptPasswordEncoder,antMatchers,springsecurity,new,password
From: https://www.cnblogs.com/romablog/p/17685425.html

相关文章

  • [SpringSecurity5.6.2源码分析三]:SpringWebMvcImportSelector
    1、SpringWebMvcImportSelector• SpringSecurity支持在SpringMVC进行参数解析的时候填充参数,支持以下的对象• 通过@AuthenticationPrincipal,获取UserDetails• 通过@CurrentSecurityContext,获取SecurityContext• 通过参数类型为CsrfToken获取CsrfToken• 究其原因是因为Spr......
  • [SpringSecurity5.6.2源码分析二]:SecurityAutoConfiguration
    • SecurityAutoConfiguration是SpringSecurity最重要的一个自动配置类• 像以前版本的教程说要在启动类上配@EnableWebSecurity,现在也是由这个自动配置类负责引入• 分析一 已经介绍了DefaultAuthenticationEventPublisher,所以说重点就只有使用@Import导入的三个类,SpringBo......
  • [SpringSecurity5.6.2源码分析一]:spring.factories下有关SpringSecurity的配置类
    1、Spring.factories• 从下图可以看出spring-boot-autoconfigure/META-INF/spring.factories中关于SpringSecurity的自动配置类有以下这些org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\org.springframework.boot.autoconfigure.securi......
  • SpringSecurity
    快速入门1.介绍springsecurity是安全框架,准确来说是安全管理框架。相比与另外一个安全框架Shiro,springsecurity提供了更丰富的功能,社区资源也比Shiro丰富springsecurity框架用于Web应用的需要进行认证​和授权​认证:验证当前访问系统的是不是本系统的用户,并且要确认具体是哪......
  • SpringSecurity简明教程
    SpringSecurity主要实现UserDetailsService来验证登录的用户信息,和Security的配置类来对登录方式和资源进行限制。案例包含利用数据库进行登录验证、URL访问限制、自定义登录页和利用ajax方式登录、实现自定义过滤器对验证码进行验证,完整代码在https://github.com/say-hey/sprin......
  • SpringSecurity基本使用流程
    本文介绍SpringBoot项目如何整合SpringSecurity,记录使用SpringSecurity完成项目的登录、退出、以及权限管理的相关流程。1、导包:导入Security,前后端交互用户凭证用的是JWT,需要导入jwt,另外登录需要用到验证码,验证码的存储需要用到redis;<!--springbootsecurity--><dependency>......
  • springSecurity异常提示国际化
    1:获取国际化文件在一个jar包里,可以先下载jar包,然后再里面找到中文的那个文件<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-core</artifactId><version>3.2.0.RELEASE</vers......
  • SpringSecurity实战笔记之OAuth
    ===================SpringSocialOAuth================一、app、小程序、前后端分离为什么使用OAuth协议1、原有方法开发繁琐、安全性和客户体验差、有些前端技术不支持cookei,如小程序2、好处:token自动生成,自定义校验,方便安全二、SpringSecurityOAuth简介1、......
  • SpringSecurity实战笔记之Security
    =================================SpringSecurity========================================一、默认配置1、默认会对所有请求都需要进行认证与授权;2、默认使用httpBasic方式进行登录3、默认的用户名为user,密码在启动应用时在console中有打印......
  • SpringSecurity实战笔记之RESTful
    =================================RESTful========================================一、JsonPath1、github:https://github.com/json-path/JsonPath二、@JsonView使用步骤(用于解决同一个对象在不同的接口返回的字段不同的场景)1、使用接口来声明多个视图2、在值对象的get方法上指......