• SpringBoot整合Spring security实现用户权限控制


    一、简介

      Spring Security 提供了基于javaEE的企业应有个你软件全面的安全服务。这里特别强调支持使用SPring框架构件的项目,Spring框架是企业软件开发javaEE方案的领导者。如果你还没有使用Spring来开发企业应用程序,我们热忱的鼓励你仔细的看一看。熟悉Spring特别是一来注入原理两帮助你更快更方便的使用Spring Security。

      人们使用Spring Secruity的原因有很多,单大部分都发现了javaEE的Servlet规范或EJB规范中的安全功能缺乏典型企业应用场景所需的深度。提到这些规范,重要的是要认识到他们在WAR或EAR级别无法移植。因此如果你更换服务器环境,这里有典型的大量工作去重新配置你的应用程序员安全到新的目标环境。使用Spring Security 解决了这些问题,也为你提供许多其他有用的,可定制的安全功能。

      正如你可能知道的两个应用程序的两个主要区域是“认证”和“授权”(或者访问控制)。这两个主要区域是Spring Security 的两个目标。“认证”,是建立一个他声明的主题的过程(一个“主体”一般是指用户,设备或一些可以在你的应用程序中执行动作的其他系统)。“授权”指确定一个主体是否允许在你的应用程序执行一个动作的过程。为了抵达需要授权的店,主体的身份已经有认证过程建立。这个概念是通用的而不只在Spring Security中。

    二、实例

    1、项目结构

    2、applcaition配置文件

    server:
      port: 8088
      servlet:
        context-path: /springboot-security-demo
        
    spring:
      security:
        user:
          name: user
          password: 123456

    3、maven依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.3.5.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    	<groupId>edu.hjl</groupId>
    	<artifactId>springboot-security-demo</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>springboot-security-demo</name>
    	<description>Demo project for Spring Boot</description>
    
    	<properties>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencies>
    	<!--devtools热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
                <scope>true</scope>
            </dependency>
    	
           <!--  springboot-->
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter</artifactId>
           </dependency>
    <!-- spring security --> <dependency>   <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.7.0</version> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

    4、继承WebSecurityConfigureAdapter配置角色权限

    package edu.hjl.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.core.userdetails.UserDetailsService;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    	@Autowired
    	private UserDetailsService userDetailsService;
    	
    	@Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    	
    	@Override
        public void configure(WebSecurity web) throws Exception {
            super.configure(web);
        }
    
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
        	auth.userDetailsService(userDetailsService)
                    .passwordEncoder(passwordEncoder());//passwoldEncoder是对密码的加密处理,如果user中密码没有加密,则可以不加此方法。注意加密请使用security自带的加密方式。
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()//禁用了 csrf 功能
                    .authorizeRequests()//限定签名成功的请求
                    .antMatchers("/decision/**","/govern/**","/employee/*").hasAnyRole("EMPLOYEE","ADMIN")//对decision和govern 下的接口 需要 EMPLOYEE 或者 ADMIN 权限
                    .antMatchers("/employee/login").permitAll()///employee/login 不限定
                    .antMatchers("/admin/**").hasRole("ADMIN")//对admin下的接口 需要ADMIN权限
                    .antMatchers("/oauth/**").permitAll()//不拦截 oauth 开放的资源
                    .anyRequest().permitAll()//其他没有限定的请求,允许访问
                    .and().anonymous()//对于没有配置权限的其他请求允许匿名访问
                    .and().formLogin()//使用 spring security 默认登录页面
                    .and().httpBasic();//启用http 基础验证
    
        }
    	
    }
    

    上面设置了两个角色:admin 和 employee;

    admin可以访问:"/decision/**","/govern/**","/employee/*","/admin/*";

    employee可以访问:"/descision/**","/govern/**","emplyoee/*";

    从配置上看admin权限大于emplyoee。

    5、SwaggerConfig配置类

    package edu.hjl.config;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
    	@Bean
        public Docket createRestApi(){
    		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any()).build();
        }
    
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("SpringBoot API Doc")
                    .description("This is a restful api document of Spring Boot.")
                    .version("1.0")
                    .build();
        }
    	
    }
    

    6、admin实体创建

    package edu.hjl.pojo;
    
    
    public class Admin {
    
    	private String username;
    	
    	private String password;
    
    	public Admin() {
    		super();
    	}
    
    	public Admin(String username, String password) {
    		super();
    		this.username = username;
    		this.password = password;
    	}
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	@Override
    	public String toString() {
    		return "Admin [username=" + username + ", password=" + password + "]";
    	}
    	
    }
    

     用于保存登陆用户信息。

    7、创建用户继承UserDetailService

    package edu.hjl.service.impl;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.stereotype.Service;
    
    import edu.hjl.pojo.Admin;
    
    @Service
    public class UserDetailsServiceImpl implements UserDetailsService {
    
    	@Override
    	public UserDetails loadUserByUsername(String username) {
    		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    		Admin admin = new Admin();
    		
    		if (username.equals("employee")) {
    			admin.setUsername("employee");
    			admin.setPassword("123456");
    			GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_EMPLOYEE");
    			grantedAuthorities.add(grantedAuthority);
    			// 创建用户,用户判断权限
    			return new User(
    					admin.getUsername(), 
    					new BCryptPasswordEncoder().encode(admin.getPassword()), 
    					grantedAuthorities);
    		} 
    		
    		if (username.equals("admin")) {
    			admin.setUsername("admin");
    			admin.setPassword("123456");
    			GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
    			grantedAuthorities.add(grantedAuthority);
    			// 创建用户,用户判断权限
    			return new User(
    					admin.getUsername(), 
    					new BCryptPasswordEncoder().encode(admin.getPassword()), 
    					grantedAuthorities);
    		}
    		
    		return null;
    	}
    
    }
    

    8、创建controller

    admin controller:

    package edu.hjl.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/admin/")
    public class AdminController {
    
    	@RequestMapping(value = "greeting", method = RequestMethod.GET)
    	public String greeting() {
    		return "hello world";
    	}
    	
    	@RequestMapping(value = "login", method = RequestMethod.GET)
    	public String login() {
    		return "login success";
    	}
    	
    }
    

     emplyoee controller:

    package edu.hjl.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/employee/")
    public class EmployeeController {
    
    	@RequestMapping(value = "greeting", method = RequestMethod.GET)
    	public String greeting() {
    		return "hello world";
    	}
    	
    	@RequestMapping(value = "login", method = RequestMethod.GET)
    	public String login() {
    		return "login success";
    	}
    	
    }
    

    9、使用Swagger接口文档进行测试

    访问地址:http://localhost:8088/springboot-security-demo/swagger-ui.html 

    点击admin-controller

    有两个我们刚写的方法,点击greeting方法,调用的时候会弹出一个form

    我们填入刚才创建的admin账号密码

    返回状态码200,登陆成功,这是访问login接口没有权限限制问题。

    除此之外,由于admin用户拥有最多访问权限,目前所有接口都可以访问,不存在权限问题,接下来我们清除缓存,用emplyoee用户登陆。

    调用 /emplyoee/greeting接口 

    输入登陆信息,登陆。

    登陆成功,再次调用 /emplyoee/login接口,也同样访问成功。

    接下来我们调用 /admin/greeting接口,看会发生什么?

    状态码返回403,此次被我们对spring security截止了,禁止访问,说明我们的配置起作用了。

    三、总结

    此次的案例,我后台是固定写死的,真实开发中用户权限的配置和访问路径是动态的,这个就需要按照各自的项目去配置。以上案例只是简单的实现此功能。

    源码地址:https://github.com/JianLiangHe/springboot-security-demo

  • 相关阅读:
    什么是Azkaban?
    设计模式 应用场景
    初识Nginx
    C++著名程序库的比较和学习经验(STL.Boost.GUI.XML.网络等等)
    C++对象模型笔记之程序设计模型
    深度探索C++对象模型之C++对象模型笔记
    对于数据库设计者而言,在设计表属性类型时应该考虑哪些问题?
    Thread 多线程 同步
    Thread sleep()休眠
    Java 多线程(Thread)学习
  • 原文地址:https://www.cnblogs.com/hejianliang/p/13929079.html
Copyright © 2020-2023  润新知