iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot 整合Security权限控制的初步配置
  • 695
分享到

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

SpringBoot整合Security配置SpringBoot Security权限控制 2022-11-13 19:11:40 695人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

正文 在源码目录下新建 config 目录, 在该目录下新建 WEBSecurityConfig 类文件 package com.edurt.config; import org.

正文

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


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;

@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配置的资料请关注编程网其它相关文章!

--结束END--

本文标题: SpringBoot 整合Security权限控制的初步配置

本文链接: https://www.lsjlt.com/news/171228.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作