日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

使用Spring Security和jdbc的Spring Boot第2部分

發布時間:2023/12/3 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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部分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。