javascript
使用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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux命令使用方法(linux 命令
- 下一篇: spring shell_Spring