Skip to content

Commit 813a56d

Browse files
author
Eugen
committed
Merge pull request eugenp#273 from Doha2012/master
spring security oauth
2 parents c8e67be + 4a24341 commit 813a56d

File tree

33 files changed

+2084
-0
lines changed

33 files changed

+2084
-0
lines changed

spring-security-oauth/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-security-oauth</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.m2e.core.maven2Builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
16+
</natures>
17+
</projectDescription>

spring-security-oauth/pom.xml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.baeldung</groupId>
4+
<artifactId>spring-security-oauth</artifactId>
5+
<version>1.0.0-SNAPSHOT</version>
6+
7+
<name>spring-security-oauth</name>
8+
<packaging>pom</packaging>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.2.7.RELEASE</version>
14+
</parent>
15+
16+
<modules>
17+
<module>spring-security-oauth-server</module>
18+
<module>spring-security-oauth-resource</module>
19+
<module>spring-security-oauth-ui</module>
20+
</modules>
21+
22+
<build>
23+
<finalName>spring-security-oauth</finalName>
24+
<pluginManagement>
25+
<plugins>
26+
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>${maven-compiler-plugin.version}</version>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-war-plugin</artifactId>
40+
<version>${maven-war-plugin.version}</version>
41+
<configuration>
42+
<failOnMissingWebXml>false</failOnMissingWebXml>
43+
</configuration>
44+
</plugin>
45+
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>${maven-surefire-plugin.version}</version>
50+
<configuration>
51+
<testFailureIgnore>true</testFailureIgnore>
52+
<excludes>
53+
<exclude>**/*IntegrationTest.java</exclude>
54+
<exclude>**/*LiveTest.java</exclude>
55+
</excludes>
56+
<systemPropertyVariables>
57+
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
58+
</systemPropertyVariables>
59+
</configuration>
60+
</plugin>
61+
62+
</plugins>
63+
</pluginManagement>
64+
</build>
65+
66+
67+
<properties>
68+
<!-- Spring -->
69+
<org.springframework.version>4.1.7.RELEASE</org.springframework.version>
70+
<org.springframework.security.version>3.2.8.RELEASE</org.springframework.security.version>
71+
<oauth.version>2.0.7.RELEASE</oauth.version>
72+
73+
<!-- marshalling -->
74+
75+
<jackson.version>2.5.1</jackson.version>
76+
77+
<!-- logging -->
78+
<org.slf4j.version>1.7.12</org.slf4j.version>
79+
<logback.version>1.1.3</logback.version>
80+
81+
<!-- util -->
82+
<guava.version>18.0</guava.version>
83+
<commons-lang3.version>3.3.2</commons-lang3.version>
84+
85+
<!-- testing -->
86+
<org.hamcrest.version>1.3</org.hamcrest.version>
87+
<junit.version>4.11</junit.version>
88+
<mockito.version>1.10.19</mockito.version>
89+
90+
<httpcore.version>4.4</httpcore.version>
91+
<httpclient.version>4.4</httpclient.version>
92+
93+
<rest-assured.version>2.4.0</rest-assured.version>
94+
95+
<!-- Maven plugins -->
96+
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
97+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
98+
<maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
99+
<cargo-maven2-plugin.version>1.4.16</cargo-maven2-plugin.version>
100+
101+
</properties>
102+
103+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
34+
</attributes>
35+
</classpathentry>
36+
<classpathentry kind="output" path="target/classes"/>
37+
</classpath>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-security-oauth-resource</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.wst.validation.validationbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>org.eclipse.m2e.core.maven2Builder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
</buildSpec>
39+
<natures>
40+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
41+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
42+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
43+
<nature>org.eclipse.jdt.core.javanature</nature>
44+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
45+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
46+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
47+
</natures>
48+
</projectDescription>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>spring-security-oauth-resource</artifactId>
5+
<name>spring-security-oauth-resource</name>
6+
<packaging>war</packaging>
7+
8+
<parent>
9+
<groupId>org.baeldung</groupId>
10+
<artifactId>spring-security-oauth</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-jdbc</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>mysql</groupId>
27+
<artifactId>mysql-connector-java</artifactId>
28+
<scope>runtime</scope>
29+
</dependency>
30+
<!-- oauth -->
31+
<dependency>
32+
<groupId>org.springframework.security.oauth</groupId>
33+
<artifactId>spring-security-oauth2</artifactId>
34+
<version>${oauth.version}</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.apache.commons</groupId>
39+
<artifactId>commons-lang3</artifactId>
40+
<version>${commons-lang3.version}</version>
41+
</dependency>
42+
43+
44+
45+
</dependencies>
46+
47+
48+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.PropertySource;
9+
import org.springframework.core.env.Environment;
10+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
11+
import org.springframework.security.oauth2.provider.token.TokenStore;
12+
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
13+
14+
@Configuration
15+
@PropertySource({ "classpath:persistence.properties" })
16+
public class OAuth2ResourceConfig {
17+
18+
@Autowired
19+
private Environment env;
20+
21+
@Bean
22+
public DataSource dataSource() {
23+
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
24+
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
25+
dataSource.setUrl(env.getProperty("jdbc.url"));
26+
dataSource.setUsername(env.getProperty("jdbc.user"));
27+
dataSource.setPassword(env.getProperty("jdbc.pass"));
28+
return dataSource;
29+
}
30+
31+
@Bean
32+
public TokenStore tokenStore() {
33+
return new JdbcTokenStore(dataSource());
34+
}
35+
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.web.SpringBootServletInitializer;
6+
7+
@SpringBootApplication
8+
public class ResourceApplication extends SpringBootServletInitializer {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(ResourceApplication.class, args);
12+
}
13+
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
5+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
6+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
7+
import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;
8+
9+
@Configuration
10+
@EnableGlobalMethodSecurity(prePostEnabled = true)
11+
public class ResourceSecurityConfig extends GlobalMethodSecurityConfiguration {
12+
13+
@Override
14+
protected MethodSecurityExpressionHandler createExpressionHandler() {
15+
return new OAuth2MethodSecurityExpressionHandler();
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
@Configuration
9+
@EnableWebMvc
10+
@ComponentScan({ "org.baeldung.web.controller" })
11+
public class ResourceWebConfig extends WebMvcConfigurerAdapter {
12+
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.web.controller;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
5+
6+
import org.baeldung.web.dto.Foo;
7+
import org.springframework.security.access.prepost.PreAuthorize;
8+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.ResponseBody;
14+
15+
@Controller
16+
@EnableResourceServer
17+
public class FooController {
18+
19+
public FooController() {
20+
super();
21+
}
22+
23+
// API - read
24+
@PreAuthorize("#oauth2.hasScope('read')")
25+
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
26+
@ResponseBody
27+
public Foo findById(@PathVariable final long id) {
28+
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)