當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用Spring Security和jdbc的Spring Boot第2部分
生活随笔
收集整理的這篇文章主要介紹了
使用Spring Security和jdbc的Spring Boot第2部分
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在上一篇文章中,我們基于Spring Security發出請求的默認表架構實現了安全性。
考慮到用戶和角色,應用程序開發人員使用適合其需求的架構。 Spring使我們能夠指定所需的查詢,以便檢索用戶名,密碼和角色等信息。
我們的自定義表將與第一個示例的表完全不同。
drop table if exists Custom_Users; create table Custom_Users(id bigint auto_increment, username varchar(255), password varchar(255)); insert into Custom_Users(username,password) values('TestUser','TestPass');drop table if exists Custom_Roles; create table Custom_Roles(username varchar(255),authority varchar(255), UNIQUE(username,authority)); insert into Custom_Roles(username,authority) values('TestUser','superadmin');為了在Spring Security中使用這些表,我們必須傳遞Spring Security將使用的查詢,以檢索所需的安全信息。
為此,我們將創建一個安全配置,該安全配置將設置所需的查詢。
package com.gkatzioura.spring.security.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; 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/20/16.*/ @EnableWebSecurity @Profile("customquery") public class CustomQuerySecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Autowiredpublic void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username,password,1 FROM Custom_Users where username=?").authoritiesByUsernameQuery("SELECT username,authority FROM Custom_Roles where username=?");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().logout().permitAll();}}我們使用彈簧輪廓。 我們的spring配置文件將是“ customquery”,因此CustomQuerySecurityConfig將綁定到“ customquery”配置文件。
為了運行,出于方便起見,我們必須在build.gradle文件中更改默認配置文件。
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" }bootRun {systemProperty "spring.profiles.active", "customquery" }運行應用程序問題
gradle bootRun您可以在github上找到源代碼
翻譯自: https://www.javacodegeeks.com/2016/09/spring-boot-spring-security-jdbc-part-2.html
總結
以上是生活随笔為你收集整理的使用Spring Security和jdbc的Spring Boot第2部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果电脑系统重装方法教程如何重装苹果电脑
- 下一篇: cometd_CometD:Java W