PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB
转自:http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/
PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB
Spring Boot provides utilities for quick and easy setup of Spring Security via auto-configuration and Java-based configuration. The getting started guide is quick and easy leads through configuring an in-memory
AuthenticationManager in just minutes. Going beyond these examples, this installation will quickly review the getting started guide provided at Spring.io, and conclude with the configuration
of a datasource-backed AuthenticationManager that uses Spring Data JPA, and the MySQL database platform.
As usual, for this installment, I’ve created a copy of the code from Part 4 and created a new project called Part 5. It’s committed to Github,
ready for cloning.
Updating Dependencies
To install Spring Security, we first need to update our gradle script to include a dependency on spring-boot-starter-security. Update build.gradle to include the following dependency as seen below.
/build.gradle:
|
1
2
3
4
5
6
7
|
dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-security") compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE") testCompile("junit:junit")} |
Following that, executing a build should pull in our new dependencies.
Creating The Security Configuration
Continuing to lift code from the Spring.io docs for review, below you’ll find the example of the base Java security configuration. We’ll review the important bits after the jump. We’ll
create this in the same directory as our other configuration files:
/src/main/java/com.rodenbostel.sample/SecurityConfiguration.java:
|
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
|
packagecom.rodenbostel.sample;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.security.authentication.AuthenticationManager;importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;importorg.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;importorg.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;importorg.springframework.security.web.util.matcher.AntPathRequestMatcher;@Configuration@EnableWebMvcSecurity@EnableGlobalMethodSecurity(prePostEnabled =true)publicclassSecurityConfigurationextendsWebSecurityConfigurerAdapter { @Override protectedvoidconfigure(HttpSecurity http)throwsException { http .authorizeRequests().anyRequest().authenticated(); http .formLogin().failureUrl("/login?error") .defaultSuccessUrl("/") .loginPage("/login") .permitAll() .and() .logout().logoutRequestMatcher(newAntPathRequestMatcher("/logout")).logoutSuccessUrl("/login") .permitAll(); } @Override protectedvoidconfigure(AuthenticationManagerBuilderthrowsException { auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); }} |
As usual, the @Configuration annotation lets Spring know that this file contains configuration information. The next two annotations (@EnableWebMvcSecurity and @EnableGlobalMethodSecurity(prePostEnabled=true))
setup the automatically-configured portions of our security scheme, provided by Spring-Boot. EnableWebMvcSecurity basically pulls in the default SpringSecurity/SpringMVC integration. It’s an extension of theWebMvcConfigurerAdapter,
and adds methods for handling and generating CSRF tokens and resolving the logged in user, and configures default AuthenticationManagers and Pre/Post object authorization implementations. The @EnableGlobalMethodSecurity sets up processors for authorization
advice that can be added around methods and classes. This authorization advice lets a developer write Spring EL that inspects input parameters and return types.
Our SecurityConfiguration class also extends WebSecurityConfigurerAdapter.
In Spring/Spring Boot, Configurer Adapters are classes that construct default bean configurations and contain empty methods which are meant to be overridden. Overriding these methods allow a developer to customize the Web Security Configuration during startup.
Typically, the default configurations are constructed, and immediately following, the empty methods are called. If you’ve overridden an empty method, you’re able to inject custom behavior into the default configuration during the startup of the container.
In our case, the two coded parts of our SecurityConfiguration class (two methods named “configure”) are examples of these empty methods meant to be overridden. During container startup, after the HttpSecurity
object’s default configuration is specified, our overridden method is called. Here we are able to customize the default configuration by specifying which requests to authorize, and how to route various security-related requests: default success URL, error
routing, where to send logouts, etc. Also during container startup, after the AuthenticationManagerBuilder is configured, our configure method is called, and in this case we’re altering the default configuration, giving instructions to the AuthenticationManagerBuilder
to build an in-memory AuthenticationManager with a default user credential and role.
You’ll notice in this configuration we’ve specified several URL paths that do not exist. There’s no login page or controller, and no way for a user to interact with the security configuration when the app is started
up. Next, we’ll need to construct and wire in a login page to complete our beginning configuration.
Building The Login Page
The login page in the Spring.io sample is very straightforward. Just a simple form with an input for username and password. Let’s build that and review a few key parts.
/src/main/resources/templates/login.html:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!DOCTYPEhtml><head> <title>Spring Security Example</title></head><body><divth:if="${param.error}"> Invalid username and password.</div><divth:if="${param.logout}"> You have been logged out.</div><formth:action="@{/login}"method="post"> <div><label>input ="text"name="username"/>label></div> <div><label>input ="password"name="password"/>label></div> <div><inputtype="submit"value="Sign/></div></form></body></html> |
Most importantly, we have inputs with the names of “username” and “password”. These are the Spring Security defaults. If you’re routing a request to Spring Security to authenticate, these are the parameters on
the request that it will be looking for. Next, you’ll also notice that there are Thymeleaf conditionals (th:if) for displaying logout and error messages if they are present in the response parameters during rendering. You’ll also notice the path to this page
is “/login”, and the action on this form routes back to “/login” – but we don’t have those registered anywhere…
Registering the Login Action
The path our login form is posting to is the default used by Spring Security. This is where what used to be called the “j_spring_security_check” servlet is listening for requests to authenticate. The request path (where we’re retrieving the login form by issuing
a GET to /login) is normally mapped to a controller, but in this case, since we’re using automatically configured features of Spring Boot, we need to specify this mapping in our application configuration. Add the code below to your application configuration.
You may notice the use of another @Override method – another hook where we can add logic to customize our application…
/src/main/java/com.rodenbostel.sample.Application.java:
|
1
2
3
4
|
@OverridepublicvoidaddViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login");} |
Log In!
Start your server, and try to access the app again. For me, that’s simply visitinghttp://localhost:8080.

I’m immediately challenged.
If I put in an invalid username or password, we should see an error:

If I put in the correct credentials (id: user/password: password), we should be able to log in:


There’s quite a bit missing here still – let’s take this example a bit further – we’ll wire in components that would make this configuration closer to production ready – an AuthenticationManager backed by JDBC,
configurable password encoders, and a UserDetailsService implementation that we can use to manage users.
Beyond The Examples
To begin taking steps closer to this solution being production-ready, we first need to back our app with a database. I’ll be using MySQL. I’ll assume you’ve got it installed and running
(if you’re on a mac, I’d use Homebrew to accomplish that.
First, we’ll add the MySQL dependency to our gradle script:
/build.gradle:
|
1
2
3
4
5
6
7
8
9
|
dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE") runtime('mysql:mysql-connector-java:5.1.6') testCompile("junit:junit")} |
Configuring A Datasource
I’ll be calling my schema in MySQL “beyond-the-examples”. I’ll assume you’ve used the same name. Conveniently, Spring Boot Starter projects have an automatically
configured property source path. This means that using a properties file for configuration data we’d like to externalize simply requires creating an “application.properties” file and putting it somewhere on the application’s classpath. We’ll create that
file now, and add properties that we’ll use to set up our datasource.
/src/main/resources/application.properties:
|
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
|
spring.datasource.url=jdbc:mysql://localhost:3306/beyond-the-examplesspring.datasource.username=rootspring.datasource.password=spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.jpa.hibernate.dialect= org.hibernate.dialect.MySQLInnoDBDialectspring.jpa.generate-ddl=false1You can see I’m using thedefaultconfigurationforMySQL. I wouldn’t recommend thatforproduction.Next, we’ll build references to these properties in our application’s configuration, so that we can use them to create a datasource bean that we can inject into our security/src/main/java/com.rodenbostel.sample.Application.java:1 @Value("${spring.datasource.driverClassName}") privateString databaseDriverClassName; @Value("${spring.datasource.url}") privateString datasourceUrl; @Value("${spring.datasource.username}") privateString databaseUsername; @Value("${spring.datasource.password}") privateString databasePassword;1Next create a Datasource@Beanusing these properties in the same file./src/main/java/com.rodenbostel.sample.Application.java:1 @Bean publicDataSource datasource() { org.apache.tomcat.jdbc.pool.DataSource ds =neworg.apache.tomcat.jdbc.pool.DataSource(); ds.setDriverClassName(databaseDriverClassName); ds.setUrl(datasourceUrl); ds.setUsername(databaseUsername); ds.setPassword(databasePassword); returnds; } |
Now, we have a datasource configured that we can @Autowire into any of our Spring beans, configuration or otherwise.
Create the Spring Security Tables
The DDL from the Spring.io docs is for HSQLDB. It’s syntax is not compliant with MySQL. Shout out to this guy (http://springinpractice.com/2010/07/06/spring-security-database-schemas-for-mysql)
for publishing the MySQL versions of the default Spring Security schema. If you’re using MySQL like me, use the DDL from that blog to create a “users” table and an “authorities” table, then thank him. Since we’ll be properly encoding our passwords, we may
want to make that password column a bit wider. Here’s what I ran:
|
1
|
createtableusers ( usernamevarchar(50)notnullprimary , passwordvarchar(255)notnull, notnull) engine = InnoDb;createtableauthoritiesvarchar(50)notnull, authorityvarchar(50)notnull, foreignkey(username)referencesusersuniqueindexauthorities_idx_1 (username, authority)) engine = InnoDb; |
Building The New Configuration
To start using the new datasource in the security configuration, we first need to wire the datasource bean into our SecurityConfiguration class. Update your SecurityConfiguration file to instruct spring to @Autowire this bean:
/src/main/java/com.rodenbostel.sample.SecurityConfiguration.java:
|
1
2
|
@AutowiredprivateDataSource datasource; |
Next, we’re going to make a few significant changes to our AuthenticationManagerBuilder configuration to reference this datasource and a few other things, which I’ll review after the code:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Override protectedvoidconfigure(AuthenticationManagerBuilderthrowsException { JdbcUserDetailsManager userDetailsService =newJdbcUserDetailsManager(); userDetailsService.setDataSource(datasource); PasswordEncoder encoder =newBCryptPasswordEncoder(); auth.userDetailsService(userDetailsService).passwordEncoder(encoder); auth.jdbcAuthentication().dataSource(datasource); if(!userDetailsService.userExists("user")) List<GrantedAuthority> authorities =newArrayList<GrantedAuthority>(); authorities.add(newSimpleGrantedAuthority("USER")); User userDetails =newUser("user","password"), authorities); userDetailsService.createUser(userDetails); } }1Prior tothis, our AuthenticationManagerBuilder was configured on a single line - we were using1auth.inMemoryAuthentication() |
to using:
|
1
|
auth.jdbcAuthentication().dataSource(datasource); |
Assuming there are already users in the database, believe it or not, that’s all we need to begin using the JDBC-backed AuthenticationManager. The requirement for creating new users and managing existing users
is a foregone conclusion. In our case, we’d like to automatically configure a default user on app startup just like we were before. We can get a handle on the automatically configuration (by Spring Boot) UserDetailsService through our AuthenticationManagerBuilder
at:
|
1
|
auth.getDefaultUserDetailsService(); |
…but that doesn’t quite do everything we need. On the first line of our updated AuthenticationManagerBuilder configuration method, you can see we’ve created a new instance of one of the provide implementations
of UserDetailsService provided by Spring. If you don’t have a reason to customize how you manage users in your system, that is a perfectly suitable implementation, but there are things to consider. Please consult the API docs for more detail (http://docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/apidocs/org/springframework/security/provisioning/JdbcUserDetailsManager.html).
After creating the new reference to the JdbcUserDetailsManager, we need to set a reference to our datasource on it. Following that, we add our encoder for storing our passwords securely, and then we use the JdbcUserDetailsManager’s built-in functionality to
check to see if our test user exists, and create him if he doesn’t.
Testing Again
Running the application should yield no change in behavior when compared with what we saw earlier. This is desired. What we will see that’s different will be in our database. Startup the app using: “gradle bootRun”, and using your favorite database management
tool, query the database to see our newly create user and their encoded password:

Conclusion
I cobbled the information in this post from many sources – some I’ve remembered and have mentioned, and others I have not. I hope putting this information in a single post helps whoever stumbles upon it! That concludes this series of Spring Boot posts, but
during the time I’ve been writing these, I’ve come up with two more topics to touch on, mostly surrounding further securing your app (http://www.jasypt.org/) and easier maintenance
of your database tables (http://flywaydb.org/). Check back soon!
PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB的更多相关文章
- Keycloak 团队宣布他们正在弃用大多数 Keycloak 适配器,包括Spring Security和Spring Boot
2月14日,Keycloak 团队宣布他们正在弃用大多数 Keycloak 适配器. 其中包括Spring Security和Spring Boot的适配器,这意味着今后Keycloak团队将不再提供 ...
- Spring Security +Oauth2 +Spring boot 动态定义权限
Oauth2介绍:Oauth2是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是安全的. 简单的来说,当用户登陆网站的时候,需要账号 ...
- 漫谈Spring Security 在Spring Boot 2.x endpoints中的应用(一)
Spring Boot 2.x极大简化了默认的安全配置,并不是说有很多安全相关的配置,现在你只需要提供一个WebSecurityConfigurerAdapter继承类这样一个简单的操作,Spring ...
- spring boot:用spring security加强spring boot admin的安全(spring boot admin 2.3.0 / spring boot 2.3.3)
一,spring boot admin的安全环节: 1,修改context-path,默认时首页就是admin, 我们修改这个地址可以更安全 2,配置ip地址白名单,有ip限制才安全, 我们使用了sp ...
- 微服务下前后端分离的统一认证授权服务,基于Spring Security OAuth2 + Spring Cloud Gateway实现单点登录
1. 整体架构 在这种结构中,网关就是一个资源服务器,它负责统一授权(鉴权).路由转发.保护下游微服务. 后端微服务应用完全不用考虑权限问题,也不需要引入spring security依赖,就正常的 ...
- SPRING SECURITY JAVA配置:Web Security
在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...
- spring security learning(spring in action)
1.使用Spring Security配置命名空间 spring securtiy 提供了安全性相关的命名空间,我们可以将spring security的命名空间声明添加到spring公用的配置xml ...
- spring security在spring mvc的action中获取登录人信息
@RequestMapping("/index") public ModelAndView login( @RequestParam(value = "error&quo ...
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
随机推荐
- mybatis源码阅读-SqlSessionFactory和SqlSession(三)
说明 读了3遍:https://my.oschina.net/zudajun/blog/665956 现在统一整理成笔记 并跟着源码一行一行调试 统一整理起来 SqlSession 接口定义 publ ...
- JAVA的输入输出基本操作样例
这些类的继承关系有些类似,弄一个作为样例,理解一下其中的机制. package cc.openhome; import java.io.*; public class Member { private ...
- Shallow Heap 和 Retained Heap的区别
http://blog.csdn.net/a740169405/article/details/53610689 Shallow Heap 和 Retained Heap的区别 https://i.c ...
- javascript的函数、创建对象、封装、属性和方法、继承
转自原文javascript的函数.创建对象.封装.属性和方法.继承 一,function 从一开始接触到js就感觉好灵活,每个人的写法都不一样,比如一个function就有N种写法 如:functi ...
- android中图型的阴影效果(shadow-effect-with-custom-shapes)
思路: 在自己定义shape中添加一层或多层,并错开.就可以显示阴影效果.为添加立体感,button按下的时候,仅仅设置一层.我们能够通过top, bottom, right 和 left 四个參数来 ...
- oracle 数据库中数据导出到excel
确保安装了PLSQL Developer工具.连接数据库. FIle--new--SQL window 运行查询,选中要导出的数据,右键--copy to excel. 或者 运行查询后.右键--se ...
- Linux命令(四)——文件权限管理
文件权限是指对文件的访问控制,即哪些用户或群组可以访问文件以及执行什么样的操作. 一.文件的权限 1.Linux文件类型 (1)普通文件:文本文件+数据文件+可执行的二进制文件. (2)目录文件:即文 ...
- SAP WEBSERVICE Soap中RPC-style和Document-style
RPC是以方法调用的方式描写叙述WebSerivce的,也就是说,你要说清楚调用的那个方法,以及各个參数的名称和值.要描写叙述这些东东.SOAP消息就要有一个统一的规范,指出那一部分是方法名.哪个部分 ...
- 黑马day01 笔记
一.xml语法 1.文档声明 用来声明xml的基本属性,用来指挥解析引擎怎样去解析当前xml 通常一个xml都要包括而且仅仅能包括一个文档声明 xml的文档必须在整个xml ...
- libcanbus官方主页
libcanbus canbus(CAN BUS V2.0 B)扩展格式库项目简析 注: 本文如果你已经有linux开发环境 请确保你使用本库时是tag版本号. 该库遵循的协议是SAE J1939-2 ...