使用Spring Security和jdbc的Spring Boot
Spring安全性是一个很棒的框架,可节省开发人员的大量时间和精力。 此外,它还具有足够的灵活性,可以自定义并满足您的需求。
使用JDBC和Spring Security非常容易,并且许多操作是自动化的。 这将是一个最小的展示。
gradle文件包含诸如spring-security,spring-jdbc和h2数据库之类的依赖项
group 'com.gkatzioura'
version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")}
}apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'sourceCompatibility = 1.8repositories {mavenCentral()
}dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile("org.thymeleaf:thymeleaf-spring4")compile("org.springframework.boot:spring-boot-starter-security")compile("org.springframework:spring-jdbc")compile("com.h2database:h2:1.4.192")compile("org.slf4j:slf4j-api:1.6.6")compile("ch.qos.logback:logback-core:1.1.7")compile("ch.qos.logback:logback-classic:1.1.7")testCompile "junit:junit:4.11"
}
必须创建包含某些信息的表。 这些表将具有Spring安全性查询的默认名称和列名称,以获取信息。
drop table if exists users;
create table users(id bigint auto_increment, username varchar(255), password varchar(255), enabled boolean);
insert into users(username,password,enabled) values('steve','steve',true);
insert into users(username,password,enabled) values('john','john',true);
drop table if exists authorities;
create table authorities(username varchar(255),authority varchar(255), UNIQUE(username,authority));
insert into authorities(username,authority) values('steve','admin');
insert into authorities(username,authority) values('john','superadmin');
这些sql语句将驻留在resources / schema.sql上。
第一步是创建我们的Application类
package com.gkatzioura.spring.security;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Created by gkatzioura on 9/2/16.*/
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
为了快速入门,该数据库将为h2数据库。
package com.gkatzioura.spring.security.config;import org.h2.jdbcx.JdbcDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.nio.file.Files;/*** Created by gkatzioura on 9/2/16.*/
@Configuration
public class DataSourceConfig {@Beanpublic DataSource createDataSource() {JdbcDataSource dataSource = new JdbcDataSource();dataSource.setURL("jdbc:h2:"+System.getProperty("java.io.tmpdir")+"/database");return dataSource;}}
通过指定h2数据库,我将该目录设置为临时目录内。 因此,一旦重新启动操作系统,数据库将消失。
如前所述以前一旦数据源豆已初始化弹簧JDBC会自动查找上一个schema.sql文件的文件资源文件夹。 如果文件存在,spring-jdbc将尝试执行schema.sql包含的语句。
下一步是定义我们的安全配置。 我们必须指定我们的安全性将基于jdbc。 同样,我们必须定义必须安全的端点。
package com.gkatzioura.spring.security.config;import org.springframework.beans.factory.annotation.Autowired;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import javax.sql.DataSource;/*** Created by gkatzioura on 9/2/16.*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Autowiredpublic void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource);}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().logout().permitAll();}
}
最后但并非最不重要的一点是,我们将添加具有安全端点和非安全端点的控制器
package com.gkatzioura.spring.security.controller;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;/*** Created by gkatzioura on 9/2/16.*/
@RestController
public class GreetController {private static final Logger LOGGER = LoggerFactory.getLogger(GreetController.class);@RequestMapping(path = "/public",method = RequestMethod.GET)public String sayFreeHi() {return "Greeting";}@RequestMapping(path = "/secured",method = RequestMethod.GET)public String saySecureHi() {return "Secured";}}
一旦尝试访问安全端点,将显示默认的spring security登录屏幕。
继续执行sql语句中指定的用户之一(例如,用户名:steve密码:steve)。 如果您要注销,只需点击/ login?logout端点即可。
运行带有
gradle bootRun
而且你很好。
您可以在github上找到源代码
翻译自: https://www.javacodegeeks.com/2016/09/spring-boot-spring-security-jdbc.html
使用Spring Security和jdbc的Spring Boot相关推荐
- 使用Spring Security和jdbc的Spring Boot第2部分
在上一篇文章中,我们基于Spring Security发出请求的默认表架构实现了安全性. 考虑到用户和角色,应用程序开发人员使用适合其需求的架构. Spring使我们能够指定所需的查询,以便检索用户名 ...
- spring nosql_使用Spring Security和NoSQL的Spring Boot
spring nosql 在前面的文章中,我们从一个SQL数据库提供用户和权威检索自定义查询设置弹簧安全配置. 如今,许多现代应用程序都使用NoSQL数据库. Spring安全性不是NoSQL数据库的 ...
- Spring Security实现JDBC用户登录认证
在搭建博客后端服务框架时,我采用邮件注册+Spring Security登录认证方式,结合mysql数据库,给大家展示下具体是怎么整合的. 本篇是基于上一篇:spring boot实现邮箱验证码注册 ...
- Spring Security系列教程解决Spring Security环境中的跨域问题
原创:千锋一一哥 前言 上一章节中,一一哥 给各位讲解了同源策略和跨域问题,以及跨域问题的解决方案,在本篇文章中,我会带大家进行代码实现,看看在Spring Security环境中如何解决跨域问题. ...
- 爆破专栏丨Spring Security系列教程之Spring Security的四种权限控制方式
原创:一一哥 前言: 在前面的章节中,一一哥 已经给大家介绍了Spring Security的很多功能,在这些众多功能中,我们知道其核心功能其实就是认证+授权. 在前面我们分别基于内存模型.基于默认的 ...
- spring security原理和机制 | Spring Boot 35
- 认证与授权流程与spring boot整合 spring security(1)
一 spring security 1.1 spring security的作用 Spring Security所解决的问题就是安全访问控制,而安全访问控制功能其实就是对所有进入系统的请求进行拦截 ...
- Spring Security是什么,以及如何在Spring Boot项目中整合Spring Security并且使用它,下面我们通过一个登录案例简单介绍一下Spring Security。
1.什么是Spring Security? 在了解Spring Security之前,我们是不是应该先思考一个问题,我们自己写的web案例一般都需要先登录,之后登录之后才能访问其他页面,或者说我们不同 ...
- springboot 访问html_Spring Boot中使用Spring Security进行安全控制
我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache ...
最新文章
- 打破技术型思维:产品经理的门槛在门里面
- SpringBoot 文件上传、下载、设置大小
- Execl导入问题之文本转换
- 技术绩效考量:你们可能都做错了
- C++primer 第 4 章 表达式 4.7条件运算符 4.8位运算符 4.9 sizeof运算符 4.10逗号运算符 4.11类型转换 4 . 1 2 运算符优先级表
- [Jmeter] 基本使用的总结
- Python删除文件、删除文件夹
- 使用vm14安装Linux系统
- 电阻的快速选型对开发项目非常重要
- CentOS 系统时间校准
- js中(0, function)是什么意思
- 计算机专业关于Java读书笔记_《Java8学习笔记》读书笔记(四)
- 华为通用软件开发工程师面经(业务主管面挂)
- Ubuntu卸载安装包
- Win32 API 函数列表1(格式有点乱)
- 考虑下面特殊的整数线性规划问题max求和 求和 为非负整数
- BeanCopy和BeanCopier在实际开发中的使用(属性copy效率优化)
- 电影评论情感分析-IMDB数据集 | python 利用朴素贝叶斯、神经网络模型
- ios 动画 隐藏tabbar_ios 开发怎么隐藏tabbar
- rust:错误处理 Blocking waiting for file lock on package cache