本篇教程咱们将介绍spring security的集成,为易于学习,咱们会尽可能剔除无关部分,用最小的配置集成spring security。当你学会怎么集成spring security以后,下一篇咱们将集成jwt,在那篇教程中,咱们会进一步介绍spring security,完善配置。在先后端分离的大趋势下,Java后端都是实现REST接口,因此本篇内容仅针对REST接口,如需了解不是接口的状况,可自行参考相关资料。java
spring security的实现基于servlet过滤器,在每一个请求被spring MVC处理以前,先要通过spring security过滤器,从而实现权限控制。权限控制分两部分,认证和受权,用户认证就是指登陆,有些接口要用户登陆后才能访问;受权是指根据用户角色授予不一样权限,有些接口要具备必定角色的用户才能访问,如管理相关的接口只限admin角色访问。mysql
咱们会建立几个接口,部分接口须要登陆才能访问,部分接口彻底放开,验证spring security是否成功集成。git
打开Eclipse,建立spring boot的spring starter project项目,选择菜单:File > New > Project ...
,弹出对话框,选择:Spring Boot > Spring Starter Project
,在配置依赖时,勾选web, security
,如不清楚怎样建立spring boot项目,参照教程: [spring boot hello world (restful接口)例子]。github
<?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 http://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.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.qikegu</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring能够用java配置,也可用xml配置,spring官方推荐用java配置,咱们这里用java配置。web
添加java配置文件:redis
a. 添加spring security过滤器spring
最简单的注册spring security过滤器的方法是给java配置类添加@EnableWebSecurity注解:sql
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter{ // ... }
b. 用哪一种方法加密密码明文数据库
用户密码保存到数据库时,通常避免明文而以hash值的方式保存,咱们能够为spring security配置用什么方法加密密码明文,此处使用官方推荐的BCryptPasswordEncoderapache
@Bean public PasswordEncoder myEncoder() { return new BCryptPasswordEncoder(); }
c. 配置用户信息加载
用户认证/登陆时须要加载用户信息比对用户名与密码,通常用户信息从数据库加载,此处为简便起见,在内存中建立用户信息,包括用户名、密码、用户角色信息
@Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password(myEncoder().encode("12345")).roles("ADMIN") .and() .withUser("user").password(myEncoder().encode("12345")).roles("USER"); }
d. 配置接口权限控制
经过配置HttpSecurity,能够匹配不一样的url路径,为他们指定不一样权限,在这里对于/hello3
接口做了权限限制,必须登陆后才能访问
@Override protected void configure(HttpSecurity http) throws Exception { http // 基于token,不须要csrf .csrf().disable() // 基于token,不须要session .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() // 下面开始设置权限 .authorizeRequests() // 须要登陆 .antMatchers("/hello3").authenticated() // 除上面外的全部请求所有放开 .anyRequest().permitAll(); }
package com.qikegu.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; 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.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; @Configuration @EnableWebSecurity // 添加security过滤器 public class SecurityConfig extends WebSecurityConfigurerAdapter{ // 密码明文加密方式配置 @Bean public PasswordEncoder myEncoder() { return new BCryptPasswordEncoder(); } // 认证用户时用户信息加载配置 @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password(myEncoder().encode("12345")).roles("ADMIN") .and() .withUser("user").password(myEncoder().encode("12345")).roles("USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http // 基于token,不须要csrf .csrf().disable() // 基于token,不须要session .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() // 下面开始设置权限 .authorizeRequests() // 须要登陆 .antMatchers("/hello3").authenticated() // 除上面外的全部请求所有放开 .anyRequest().permitAll(); } }
添加一个控制类,实现几个测试接口。
在项目中添加文件
添加几个接口,文件内容以下
package com.qikegu.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping(value="/hello1", method=RequestMethod.GET) public String hello1() { return "Hello1!"; } @RequestMapping(value="/hello2", method=RequestMethod.GET) public String hello2() { return "Hello2!"; } @RequestMapping(value="/hello3", method=RequestMethod.GET) public String hello3() { return "Hello3!"; } }
咱们实现了3个接口,其中/hello3
被配置为须要登陆访问,因此访问/hello3
时会返回权限受限的错误。
Eclipse左侧,在项目根目录上点击鼠标右键弹出菜单,选择:run as -> spring boot app
运行程序。 打开Postman访问接口,运行结果以下:
访问/hello1
接口,不受限
访问/hello3
接口,受限。后面教程将会介绍登陆访问接口的过程。
本文介绍了spring boot项目中集成spring security的过程,尽可能以简单的方式配置security,下一篇教程将更加详细地介绍spring security以及jwt的集成。