2018年2月24日 | Leave a comment 问题: 解决: 创建mysql表 create table users( username varchar(50) not null primary key, password varchar(200) not null, enabled int not null ); create table authorities ( username varchar(50) not null, authority varchar(200) not null, constraint fk_authorities_users foreign key(username) references users(username) ); create unique index ix_auth_username on authorities (username,authority); 1234567891011 create table users(username varchar(50) not null primary key,password varchar(200) not null,enabled int not null);create table authorities (username varchar(50) not null,authority varchar(200) not null,constraint fk_authorities_users foreign key(username) references users(username));create unique index ix_auth_username on authorities (username,authority); pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RC1</version> </parent> <groupId>demo</groupId> <artifactId>securityjdbc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <repositories> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project> 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RC1</version> </parent> <groupId>demo</groupId> <artifactId>securityjdbc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <repositories> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies></project> App.java package demo.securityjdbc; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; 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 org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User.UserBuilder; @SpringBootApplication public class App { final static Logger LOG = LoggerFactory.getLogger(App.class); @EnableWebSecurity public static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { LOG.info("configure enter..."); http.authorizeRequests() .antMatchers("/", "/test/**").permitAll() .antMatchers("/user/**").authenticated() .anyRequest().permitAll() .and() .formLogin(); } @Autowired DataSource ds; @Autowired public void configGlobal(AuthenticationManagerBuilder auth) throws Exception { final UserBuilder ub = User.withDefaultPasswordEncoder(); auth.jdbcAuthentication() .dataSource(ds) .withUser(ub.username("test1").password("123456").roles("USER")) .withUser(ub.username("test2").password("123456").roles("ADMIN")); } } public static void main(String[] argv) { SpringApplication.run(App.class, argv); } } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 package demo.securityjdbc; import javax.sql.DataSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;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 org.springframework.security.core.userdetails.User;import org.springframework.security.core.userdetails.User.UserBuilder; @SpringBootApplicationpublic class App { final static Logger LOG = LoggerFactory.getLogger(App.class); @EnableWebSecurity public static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { LOG.info("configure enter..."); http.authorizeRequests() .antMatchers("/", "/test/**").permitAll() .antMatchers("/user/**").authenticated() .anyRequest().permitAll() .and() .formLogin(); } @Autowired DataSource ds; @Autowired public void configGlobal(AuthenticationManagerBuilder auth) throws Exception { final UserBuilder ub = User.withDefaultPasswordEncoder(); auth.jdbcAuthentication() .dataSource(ds) .withUser(ub.username("test1").password("123456").roles("USER")) .withUser(ub.username("test2").password("123456").roles("ADMIN")); } } public static void main(String[] argv) { SpringApplication.run(App.class, argv); }} TestController.java package demo.securityjdbc; import java.security.Principal; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping("/test/test1") public String test1() { return "test1"; } @RequestMapping("/user/user1") public String user1(Principal p) { return String.format("user1 %s", p.getName()); } @RequestMapping("/") public String welcome(Principal p) { return String.format("welcome %s", p == null ? "" : p.getName()); } } 123456789101112131415161718192021222324 package demo.securityjdbc; import java.security.Principal;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class TestController { @RequestMapping("/test/test1") public String test1() { return "test1"; } @RequestMapping("/user/user1") public String user1(Principal p) { return String.format("user1 %s", p.getName()); } @RequestMapping("/") public String welcome(Principal p) { return String.format("welcome %s", p == null ? "" : p.getName()); }} 参考: