一直以来,Spring系列给人的感觉都是快速,简洁,好理解,易操作.但Security是一个特例,这个框架相比而言,首先就是复杂,其次是灵活性也不够.好在于是Spring出的,因此与Spring配合比较好.并且在Spring的大力推广和支持下,它仍然屹立在这里.当然它也有自己的优点,比如他与LDAP还有Oauth这些结构的集成,处理的也不错.我们今天主要从以下几个方面来分享关于Security的知识:
-
基础使用
-
与LDAP的集成
-
与OAuth2.x的集成
1. Security的基础使用
在web应用的设计中,权限是一个绕不开的话题.而在web权限设计中,RBAC是最流行的设计思路了.(除了RABC,还有像Linux中的ACL权限设计).在RBAC这种设计思路的引导下,我们可以有很多种实现方式,从最简单的一个过滤器开始,到Security或Shiro,甚至和其他的第三方进行集成,都是没有问题的.今天我们就先来看看Spring Security怎么使用.
1.1 SpringBoot中Security的默认配置
我们创建一个SpringBoot项目,然后引入spring-security,pom中的依赖如下所示:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
</dependencies>
然后我们添加一个测试的接口,如下所示:
@RestController @RequestMapping("users/") public class UserInfoController {
@GetMapping("hello") public String hello(){ return "HelloWorld"; } }
最后是我们的启动类,其实启动类并没有任何改变:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
我们启动,就会发现在日志里,他给我们生成了这样的一段内容:
2018-11-13 13:42:15.307 INFO 13084 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2018-11-13 13:42:15.620 INFO 13084 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: b74fd02a-0ad2-40ec-b6cd-3f2edfa015c1
2018-11-13 13:42:15.756 INFO 13084 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7f13811b, org.springframework.security.web.context.SecurityContextPersistenceFilter@22d7fd41, org.springframework.security.web.header.HeaderWriterFilter@4fc165f6, org.springframework.security.web.csrf.CsrfFilter@65514add, org.springframework.security.web.authentication.logout.LogoutFilter@3bc69ce9, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@1ca610a0, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@79980d8d, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@59fc6d05, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1775c4e7, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@19fd43da, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@2785db06, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@78307a56, org.springframework.security.web.session.SessionManagementFilter@5a7df831, org.springframework.security.web.access.ExceptionTranslationFilter@750f64fe, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@1f9d4b0e] 2018-11-13 13:42:15.878 INFO 13084 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-11-13 13:42:15.885 INFO 13084 --- [ main] top.lianmengtu.security.Application : Started Application in 4.07 seconds (JVM running for 4.73)
这里给我们生成了一个密码,它是做什么的呢?我们现在来访问我们的测试接口,然后就会有一个登录窗口让你登录.这是怎么回事儿呢?
http://localhost:8080/users/hello
这是因为当我们添加了security模块后,SpringBoot默认为我们启用了security的拦截,并且如果我们没有配置默认的用户名密码的话,他就给我们生成了一个默认的用户名user,而密码则就是我在上面的日志中.当我们完成登录后,我们就可以正常使用我们的接口了.
1.2 SpringBoot中SpringSecurity的简易配置
现在我们来对SpringSecurity进行自定义用户名密码配置,我们创建一个application.yml,然后设置如下:
spring: security: user: name: zhangsan password: zhangsan123
然后重启我们的应用,我们会发现,SpringBoot不在给我们提供默认密码了,而当我们访问我们的接口的时候,我们可以使用新配置的zhangsan和zhangsan123进行登录.