博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
整合spring security
阅读量:3928 次
发布时间:2019-05-23

本文共 2693 字,大约阅读时间需要 8 分钟。

spring boot 项目整合 spring security 需要pom.xml 引入

org.springframework.boot
spring-boot-starter-security
org.springframework.security
spring-security-test
test

然后在项目中创建security的Config文件

package com.springboot.wangpan.config;import org.springframework.beans.factory.annotation.Autowired;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.builders.HttpSecurity;import org.springframework.security.config.annotation.web.builders.WebSecurity;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;/** * @author homolo * @version 1.0 * @date 20-8-25 上午10:02 */@Configuration@EnableWebSecuritypublic class securityConfig extends WebSecurityConfigurerAdapter {
/** WebSecurity: 全局请求忽略规则配置(比如说静态文件,比如说注册页面)、全局HttpFirewall配置、是否debug配置、全局 SecurityFilterChain配置、privilegeEvaluator、expressionHandler、securityInterceptor; */ @Override public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers("/", "/css/**", "/img/**", "/js/**"); } /** HttpSecurity:具体的权限控制规则配置。一个这个配置相当于xml配置中的一个标签。各种具体的认证机制的相关配置, OpenIDLoginConfigurer、AnonymousConfigurer、FormLoginConfigurer、HttpBasicConfigurer等。 */ @Override public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/unregistered/**").permitAll() .antMatchers("/userService/controller/**").permitAll() .anyRequest().authenticated().and() .formLogin().loginPage("/").permitAll() .and().logout().permitAll(); httpSecurity.csrf().disable(); } /** AuthenticationManagerBuilde:用来配置全局的认证相关的信息,其实就是AuthenticationProvider和UserDetailsService, 前者是认证服务提供商,后者是用户详情查询服务; */ @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("123456")).roles("USER"); } /** 注入 PasswordEncoder bean 进行密码加密 */ @Bean public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); }}

转载地址:http://detgn.baihongyu.com/

你可能感兴趣的文章
电商系统的高并发设计和挑战
查看>>
【深入Java虚拟机】之二:Class类文件结构
查看>>
【深入Java虚拟机】之三:类初始化
查看>>
对齐数导致的错误
查看>>
thrift 实战总结
查看>>
event_base
查看>>
BufferEvent
查看>>
Evbuffer
查看>>
gcc / g++ Debug 模式
查看>>
c99:Designated Initializers(指定初始化)
查看>>
getopt函数
查看>>
线程中join()和detach()的区别
查看>>
16.让对话框支持拖拽操作/目录框打开操作
查看>>
电影天堂爬虫
查看>>
sql练习--查找所有已经分配部门的员工的last_name和first_name
查看>>
sql练习--查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
查看>>
sql练习--查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
查看>>
Transformer-based Object Re-Identification论文解读
查看>>
Android BLE开发
查看>>
Java内部类详解
查看>>