Skip to content

Commit 1083bad

Browse files
author
eugenp
committed
new demo projects
1 parent 54131bc commit 1083bad

20 files changed

Lines changed: 605 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
29+
</attributes>
30+
</classpathentry>
31+
<classpathentry kind="output" path="target/classes"/>
32+
</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-demo</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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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-demo</artifactId>
5+
<name>spring-security-oauth-resource-demo</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+
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
21+
<!-- oauth -->
22+
<dependency>
23+
<groupId>org.springframework.security.oauth</groupId>
24+
<artifactId>spring-security-oauth2</artifactId>
25+
<version>${oauth.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.apache.commons</groupId>
30+
<artifactId>commons-lang3</artifactId>
31+
<version>${commons-lang3.version}</version>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
<build>
37+
<finalName>spring-security-oauth-resource-demo</finalName>
38+
<resources>
39+
<resource>
40+
<directory>src/main/resources</directory>
41+
<filtering>true</filtering>
42+
</resource>
43+
</resources>
44+
</build>
45+
46+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
12+
13+
@Override
14+
protected MethodSecurityExpressionHandler createExpressionHandler() {
15+
return new OAuth2MethodSecurityExpressionHandler();
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.http.SessionCreationPolicy;
6+
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
7+
import org.springframework.security.oauth2.provider.token.TokenStore;
8+
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
9+
10+
//@Configuration
11+
//@EnableResourceServer
12+
public class OAuth2ResourceServerConfigDemo extends ResourceServerConfigurerAdapter {
13+
14+
//
15+
16+
@Override
17+
public void configure(final HttpSecurity http) throws Exception {
18+
// @formatter:off
19+
http
20+
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
21+
.and().authorizeRequests().anyRequest().authenticated();
22+
;
23+
// @formatter:on
24+
}
25+
26+
@Bean
27+
public TokenStore tokenStore() {
28+
return new InMemoryTokenStore();
29+
}
30+
31+
}
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 ResourceServerApplication extends SpringBootServletInitializer {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(ResourceServerApplication.class, args);
12+
}
13+
14+
}
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 ResourceServerWebConfig extends WebMvcConfigurerAdapter {
12+
//
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.Bar;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestBody;
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+
import org.springframework.web.bind.annotation.ResponseStatus;
15+
16+
@Controller
17+
public class BarController {
18+
19+
public BarController() {
20+
super();
21+
}
22+
23+
// API - read
24+
// @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('read')")
25+
@RequestMapping(method = RequestMethod.GET, value = "/bars/{id}")
26+
@ResponseBody
27+
public Bar findById(@PathVariable final long id) {
28+
return new Bar(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
29+
}
30+
31+
// API - write
32+
// @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
33+
@RequestMapping(method = RequestMethod.POST, value = "/bars")
34+
@ResponseStatus(HttpStatus.CREATED)
35+
@ResponseBody
36+
public Bar create(@RequestBody final Bar bar) {
37+
bar.setId(Long.parseLong(randomNumeric(2)));
38+
return bar;
39+
}
40+
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.http.HttpStatus;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestBody;
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+
import org.springframework.web.bind.annotation.ResponseStatus;
15+
16+
@Controller
17+
public class FooController {
18+
19+
public FooController() {
20+
super();
21+
}
22+
23+
// API - read
24+
// @PreAuthorize("#oauth2.hasScope('foo') and #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+
// API - write
32+
// @PreAuthorize("#oauth2.hasScope('foo') and #oauth2.hasScope('write')")
33+
@RequestMapping(method = RequestMethod.POST, value = "/foos")
34+
@ResponseStatus(HttpStatus.CREATED)
35+
@ResponseBody
36+
public Foo create(@RequestBody final Foo foo) {
37+
foo.setId(Long.parseLong(randomNumeric(2)));
38+
return foo;
39+
}
40+
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.web.dto;
2+
3+
public class Bar {
4+
private long id;
5+
private String name;
6+
7+
public Bar() {
8+
super();
9+
}
10+
11+
public Bar(final long id, final String name) {
12+
super();
13+
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
//
19+
20+
public long getId() {
21+
return id;
22+
}
23+
24+
public void setId(final long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(final String name) {
33+
this.name = name;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)