springboot项目搭建开发(三)

2020-06-14  本文已影响0人  雨落流年

前言

通过前文,一个简单的springboot项目已经搭建完毕。在此基础上,我们来搭建一个简单的权限管理,做到具体的路由拦截。通过本文,你将了解Spring Security在Springboot上的初步搭建。

1. pox.xml 加入如下依赖
<!-- 权限集成 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 权限测试 -->
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-test</artifactId>
  <scope>test</scope>
</dependency>
2.Spring Security配置
package org.yh.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.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
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .passwordEncoder(passwordEncoder())
                .withUser("user")
                .password(passwordEncoder().encode("miClave"))
                .roles("USER");

    }
    // 使用工作因数的加密算法
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

3.浏览器运行,Spring Security启动成功

上一篇下一篇

猜你喜欢

热点阅读