SpringBoot 整合Security权限控制的初步配置

 更新时间:2022年11月10日 15:50:20   作者:EdurtIO  
这篇文章主要为大家介绍了SpringBoot 整合Security权限控制的初步配置实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

正文

在源码目录下新建 config 目录, 在该目录下新建 WebSecurityConfig 类文件

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
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.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/**
 * WebSecurityConfig <br/>
 * 描述 : WebSecurityConfig <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 创建时间 : 2018-03-15 下午3:18 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com" rel="external nofollow" >qianmoQ</a>
 */
@Configuration
// 开启security访问授权
@EnableWebSecurity
// 开启security注解模式
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 允许直接访问/路径
        http.authorizeRequests().antMatchers("/").permitAll()
                // 其他路径需要授权访问
                .anyRequest().authenticated()
                // 指定登录页面
                .and().formLogin().loginPage("/user/login")
                // 登录成功后的默认路径
                .defaultSuccessUrl("/").permitAll()
                // 退出登录后的默认路径
                .and().logout().logoutSuccessUrl("/user/login").permitAll();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // 配置用户登录检索策略
        auth.userDetailsService(userDetailsService())
                // 配置密码策略
                .passwordEncoder(passwordEncoder());
//        auth.inMemoryAuthentication().withUser("user").password("123456").roles("USER")
//                .and().withUser("admin").password("123456").roles("ADMIN");
    }
    @Bean
    public Md5PasswordEncoder passwordEncoder() {
        return new Md5PasswordEncoder();
    }
    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        // 创建模拟用户
        manager.createUser(User.withUsername("user").password("123456").roles("USER").build());
        manager.createUser(User.withUsername("admin").password("123456").roles("ADMIN").build());
        return manager;
    }
}

浏览器打开 http://localhost:8080/home 会自动跳转到用户登录页面, 输入账号和密码出现账号密码校验页面

以上就是SpringBoot 整合Security权限控制的初步配置的详细内容,更多关于SpringBoot整合Security配置的资料请关注脚本之家其它相关文章!

相关文章

  • 创建好SpringBoot项目后但是找不到Maven的解决方法

    创建好SpringBoot项目后但是找不到Maven的解决方法

    在使用IDEA专业版创建好SpringBoot项目后,发现上方导航栏的运行按钮是灰色的,而且左侧导航栏的pom.xml的图标颜色也不是正常的,点开右侧导航栏的Maven后,发现Maven找不到,所以本文介绍了创建好SpringBoot项目后但是找不到Maven的解决方法,需要的朋友可以参考下
    2024-10-10
  • RabbitMQ消费者限流实现消息处理优化

    RabbitMQ消费者限流实现消息处理优化

    这篇文章主要介绍了RabbitMQ消费者限流实现消息处理优化,消费者限流是用于消费者每次获取消息时限制条数,注意前提是手动确认模式,并且在手动确认后才能获取到消息,感兴趣想要详细了解可以参考下文
    2023-05-05
  • JAVA通过Filter实现允许服务跨域请求的方法

    JAVA通过Filter实现允许服务跨域请求的方法

    这里的域指的是这样的一个概念:我们认为若协议 + 域名 + 端口号均相同,那么就是同域即我们常说的浏览器请求的同源策略。这篇文章主要介绍了JAVA通过Filter实现允许服务跨域请求,需要的朋友可以参考下
    2018-11-11
  • java中判断字段真实长度的实例(中文2个字符,英文1个字符)

    java中判断字段真实长度的实例(中文2个字符,英文1个字符)

    下面小编就为大家带来一篇java中判断字段真实长度的实例(中文2个字符,英文1个字符)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Spring Boot集成Sorl搜索客户端的实现代码

    Spring Boot集成Sorl搜索客户端的实现代码

    本篇文章主要介绍了Spring Boot集成Sorl搜索客户端的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • Java后台开发之表单提交之前验证

    Java后台开发之表单提交之前验证

    这篇文章主要介绍了Java后台开发之表单提交之前验证的实现代码,非常不错具有参考借鉴价值,需要的朋友参考下吧
    2017-02-02
  • IDEA连接远程服务器简化部署流程

    IDEA连接远程服务器简化部署流程

    笔者每次上线部署应用,都要使用第三方的客户端连接工具,比如 Xshell,FinalShell,Terminus 等,基本的流程步骤及其繁琐,基于这个原因,笔者今天探索通过 IDEA 连接远程服务器并上传文件,减少繁琐的部署步骤,需要的朋友可以参考下
    2024-01-01
  • java的Jackson将json字符串转换成泛型List

    java的Jackson将json字符串转换成泛型List

    这篇文章主要介绍了java的Jackson将json字符串转换成泛型List ,这里整理了详细的代码,有需要的小伙伴可以参考下。
    2017-02-02
  • Java解析使用JSON的多种方法

    Java解析使用JSON的多种方法

    使用JSON作为数据传输,在浏览器端非常方便。JSON去除了所有JavaScript执行代码,只保留对象格式,而且JSON天生适合JavaScript处理,所以,绝大多数REST API都选择JSON作为数据传输格式。现在问题来了:使用Java如何对JSON进行读写?
    2022-12-12
  • 使用Java自制一个一个Nacos

    使用Java自制一个一个Nacos

    Nacos是 Dynamic Naming and Configuration Service的首字母简称,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,本文将尝试用Java实现一个Nacos,感兴趣的可以了解下
    2024-01-01

最新评论