在上一篇文章中,我们基于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