服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Spring Boot2.0使用Spring Security的示例代码

Spring Boot2.0使用Spring Security的示例代码

2021-05-26 13:40大魔王先生 Java教程

这篇文章主要介绍了Spring Boot2.0使用Spring Security的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、spring secutity简介

spring 是一个非常流行和成功的 java 应用开发框架。spring security 基于 spring 框架,提供了一套 web 应用安全性的完整解决方案。一般来说,web 应用的安全性包括用户认证(authentication)和用户授权(authorization)两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。

对于上面提到的两种应用情景,spring security 框架都有很好的支持。在用户认证方面,spring security 框架支持主流的认证方式,包括 http 基本认证、http 表单验证、http 摘要认证、openid 和 ldap 等。在用户授权方面,spring security 提供了基于角色的访问控制和访问控制列表(access control list,acl),可以对应用中的领域对象进行细粒度的控制。

另外spring security也集成了oauth2.0,接下来我们就介绍下这两种使用的,当然spring security还集成cas等等,如果你要了解更多请查看 官方文档 ,我们下面的都是使用spring boot2.0做的demo,2.0以后已经集成了spring security5.0以上的版本;

二、basic认证

这个也是我们经常使用的基于表单的认证,输入一个账号和密码点击登录这种,就是basic认证,我们接下主要会讲一下使用以及5.0以后做了那些升级;

1.使用以及常用的一些参数的介绍

第一步使用maven引入spring security jia包,这里我们使用thymeleaf作为前端模板页面,这里也有一个地方可以探讨一波,为什么spring mvc可以自由切换模板,这个地方我们找个机会一起探讨,这里先做下简单的介绍;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<dependencies>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter</artifactid>
 </dependency>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-security</artifactid>
 </dependency>
 <!-- 前端模板 thymeleaf 依赖 -->
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-thymeleaf</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.boot</groupid>
  <artifactid>spring-boot-starter-thymeleaf</artifactid>
 </dependency>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-security</artifactid>
 </dependency>
 </dependencies>

第二步引入java配置配置方案

这里我们先使用@configuration和@enablewebsecurity对java类进行配置,接下来我们就是继承websecurityconfigureradapter,对里面的方法重写就可以了,分别是对authenticationmanagerbuilder,websecurity,httpsecurity方法,我们主要介绍authenticationmanagerbuilder和httpsecurity,通过对这两种方法重写最终实现我们自定义认证;

先来介绍一下httpsecurity常用参数,如下图用法基本脱离不了下面这些方法,可以基于认证的方式有formlogin、openidlogin、oauth2login,还可以做一些记住账号操作rememberme,还可以进行session配置管理,还支持登出loginout等等,使用起来还是蛮简单的,大家可以参照一下这篇文章,还是蛮详细的;

Spring Boot2.0使用Spring Security的示例代码

接下来我们再看下authenticationmanagerbuilder,我们重写这个方法,可以基于内存用户认证、数据库认证、ldap认证、还可以自定义用户服务、还可以自己定义认证。这里我们使用自定义认证的做demo,另外这个大家还可能有一个困惑的点,configglobal和configure的差别在哪里,这里大家可以 参考下这篇文章 ,spring security从3.2版本以后就默认开启了crsf防护,这里是通过token方式去检测的,在登陆的时候thymeleaf模板会生成_csrf的标签来防止crsf,对csrf不懂的大家可以看下这篇文章 ,这个里面介绍一些防护csrf的手段,大家可以思考下,我的demo只是一个简单的架子,为了是给大家介绍一些知识,可扩展大家根据这些介绍的知识可以随心所欲的扩展自己想要的,不要拘泥于一种方法;

最后我还要介绍一下加密的方式,spring security 4的时候我们常用的加密方式是md5加盐的方式,5.0以后版本就找不到md5passwordencoder,说明这个方法还是不够安全,还是可以通过暴力破解可以搞定,可能我不行但是拦不住一些高手,大家可以看下官方支持的以及弃用一些方法:

Spring Boot2.0使用Spring Security的示例代码

使用我就不介绍了,有兴趣可以自己探索一波,还可以参考一下 文章一 、文章二 ,下面我粘贴我的代码,讲到的这些可以扩展的地方大家我在代码中会标识清楚,喜欢动手可以尝试一下,我的重点是oauth2验证;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
 *自定义认证
 * created by wangt on 2018/7/29.
 */
@configuration
@enablewebsecurity
@enableglobalmethodsecurity
public class websecurityconfig extends websecurityconfigureradapter {
 
 /**
 * http资源认证
 * @param http
 * @throws exception
 */
 @override
 protected void configure(httpsecurity http) throws exception {
 http.authorizerequests()
  .antmatchers("/","/home").permitall()
  .anyrequest().authenticated()
  .and()
  .formlogin()
  .loginpage("/login")
  .permitall()
  .and()
  .logout()
  .permitall();
 }
 
 /**
 * 自定义认证策略
 */
 @autowired
 public void configglobal(authenticationmanagerbuilder auth) throws exception {
 auth.authenticationprovider(authprovider()).erasecredentials(true);
 }
 
 @bean
 public authprovider authprovider(){
 return new authprovider();
 }
}
 
 
/**
 * 自定义认证
 * created by wangt on 2018/8/18.
 */
public class authprovider implements authenticationprovider {
 private final bcryptpasswordencoder bcryptpasswordencoder=new bcryptpasswordencoder();
 
 @override
 public authentication authenticate(authentication authentication) throws authenticationexception {
 string username = authentication.getname();
 string inputpassword = (string) authentication.getcredentials();
 
 //如果你要使用thymeleaf认证方言可以扩展一下user
 //grantedauthority这个是使用方言的属性,有兴趣了解下
 //其实也就是通过这个使用if去判断
 user user =new user();
 user.setname("admin");
 user.setpassword("admin");
 if (user == null) {
  throw new authenticationcredentialsnotfoundexception("autherror");
 }
 
 //这一块可以自定义一些加密方式
 //自己动手实现一下
 if (true) {
  //这块有好几个构造
  //如果使用方言你可以使用3个参数的构造函数
  return new usernamepasswordauthenticationtoken(user, null);
 
 }
 
 throw new badcredentialsexception("autherror");
 }
 
 @override
 public boolean supports(class<?> aclass) {
 return true;
 }
}
 
 
/**
 * created by wangt on 2018/8/18.
 */
@configuration
public class webmvcconfig implements webmvcconfigurer {
 
 //如果使用thymeleaf方言在这块扩展
 
 
 @override
 public void addviewcontrollers(viewcontrollerregistry registry) {
 registry.addviewcontroller("/index").setviewname("index");
 registry.addviewcontroller("/").setviewname("index");
 registry.addviewcontroller("/hello").setviewname("hello");
 registry.addviewcontroller("/login").setviewname("login");
 }
}
 
 
<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>title</title>
</head>
<body>
<!--主页面 index-->
您好!欢迎光临!
<a href="/login">登录</a>
<a href="/hello">限制访问的页面</a>
</body>
</html>
 
 
<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>title</title>
</head>
<body>
<!--hello页面-->
hello
</body>
</html>
 
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="utf-8">
 <title>title</title>
</head>
<body>
<!--登录页面-->
<form th:action="@{/login}" method="post">
 <div><label> user name : <input type="text" name="username"/> </label></div>
 <div><label> password: <input type="password" name="password"/> </label></div>
 <div><input type="submit" value="登录"/></div>
</form>
 
</body>
</html>
 
/**
 * 登录控制器
 * created by wangt on 2018/8/18.
 */
@controller
public class logincontroller {
 
 @getmapping("/login")
 public string login(){
 return "/login";
 }
 
}
 
/**
 * 主页
 * created by wangt on 2018/7/28.
 */
@controller
public class homecontroller {
 
 @getmapping("/")
 public string index(){
  return "index";
 }
}
 
/**
 * hello页
 * created by wangt on 2018/8/19.
 */
@controller
public class hellocontroller {
 
 @getmapping("/hello")
 public string index(){
 return "hello";
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/wtzbk/p/9387859.html

延伸 · 阅读

精彩推荐