Skip to content

Commit 5e5c2ef

Browse files
committed
add password grant oauth2
1 parent d7bb016 commit 5e5c2ef

File tree

26 files changed

+396
-51
lines changed

26 files changed

+396
-51
lines changed

spring-security-oauth/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
<modules>
1717
<module>spring-security-oauth-server</module>
1818
<module>spring-security-oauth-resource</module>
19-
<module>spring-security-oauth-ui</module>
19+
<module>spring-security-oauth-ui-implicit</module>
20+
<module>spring-security-oauth-ui-password</module>
2021
</modules>
2122

2223
<build>

spring-security-oauth/spring-security-oauth-resource/pom.xml

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,45 @@
1111
<version>1.0.0-SNAPSHOT</version>
1212
</parent>
1313

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-
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+
</dependencies>
44+
45+
<build>
46+
<finalName>spring-security-oauth-resource</finalName>
47+
<resources>
48+
<resource>
49+
<directory>src/main/resources</directory>
50+
<filtering>true</filtering>
51+
</resource>
52+
</resources>
53+
</build>
4754

4855
</project>

spring-security-oauth/spring-security-oauth-server/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444

4545

4646
<build>
47+
<finalName>spring-security-oauth-server</finalName>
48+
<resources>
49+
<resource>
50+
<directory>src/main/resources</directory>
51+
<filtering>true</filtering>
52+
</resource>
53+
</resources>
4754
<plugins>
4855
<plugin>
4956
<groupId>org.springframework.boot</groupId>

spring-security-oauth/spring-security-oauth-server/src/main/java/org/baeldung/config/AuthServerOAuth2Config.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.sql.DataSource;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
67
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.context.annotation.Bean;
89
import org.springframework.context.annotation.Configuration;
@@ -31,6 +32,7 @@ public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter
3132
private Environment env;
3233

3334
@Autowired
35+
@Qualifier("authenticationManagerBean")
3436
private AuthenticationManager authenticationManager;
3537

3638
@Value("classpath:schema.sql")
@@ -47,8 +49,14 @@ public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
4749
clients.jdbc(dataSource())
4850
.withClient("clientId")
4951
.authorizedGrantTypes("implicit")
50-
.scopes("read","write")
51-
.autoApprove(true);
52+
.scopes("read")
53+
.autoApprove(true)
54+
.and()
55+
.withClient("clientIdPassword")
56+
.secret("secret")
57+
.authorizedGrantTypes("password","authorization_code", "refresh_token")
58+
.scopes("read");
59+
5260
// @formatter:on
5361
}
5462

spring-security-oauth/spring-security-oauth-server/src/main/java/org/baeldung/config/ServerSecurityConfig.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.baeldung.config;
22

3+
import org.springframework.context.annotation.Bean;
34
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.authentication.AuthenticationManager;
46
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
57
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
68
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -11,17 +13,24 @@ public class ServerSecurityConfig extends WebSecurityConfigurerAdapter {
1113
@Override
1214
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
1315
auth.inMemoryAuthentication().withUser("john").password("123").roles("USER");
16+
17+
}
18+
19+
@Override
20+
@Bean
21+
public AuthenticationManager authenticationManagerBean() throws Exception {
22+
return super.authenticationManagerBean();
1423
}
1524

1625
@Override
1726
protected void configure(HttpSecurity http) throws Exception {
1827
// @formatter:off
19-
http
20-
.authorizeRequests()
28+
http.authorizeRequests()
2129
.antMatchers("/login").permitAll()
2230
.anyRequest().authenticated()
2331
.and()
2432
.formLogin().permitAll();
2533
// @formatter:on
2634
}
35+
2736
}

spring-security-oauth/spring-security-oauth-ui/.classpath renamed to spring-security-oauth/spring-security-oauth-ui-implicit/.classpath

File renamed without changes.

spring-security-oauth/spring-security-oauth-ui/.project renamed to spring-security-oauth/spring-security-oauth-ui-implicit/.project

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>spring-security-oauth-ui</name>
3+
<name>spring-security-oauth-ui-implicit</name>
44
<comment></comment>
55
<projects>
66
</projects>
@@ -21,17 +21,17 @@
2121
</arguments>
2222
</buildCommand>
2323
<buildCommand>
24-
<name>org.springframework.ide.eclipse.core.springbuilder</name>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
2525
<arguments>
2626
</arguments>
2727
</buildCommand>
2828
<buildCommand>
29-
<name>org.eclipse.wst.validation.validationbuilder</name>
29+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
3030
<arguments>
3131
</arguments>
3232
</buildCommand>
3333
<buildCommand>
34-
<name>org.eclipse.m2e.core.maven2Builder</name>
34+
<name>org.eclipse.wst.validation.validationbuilder</name>
3535
<arguments>
3636
</arguments>
3737
</buildCommand>

spring-security-oauth/spring-security-oauth-ui/pom.xml renamed to spring-security-oauth/spring-security-oauth-ui-implicit/pom.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<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">
22
<modelVersion>4.0.0</modelVersion>
3-
<artifactId>spring-security-oauth-ui</artifactId>
3+
<artifactId>spring-security-oauth-ui-implicit</artifactId>
44

5-
<name>spring-security-oauth-ui</name>
5+
<name>spring-security-oauth-ui-implicit</name>
66
<packaging>war</packaging>
77

88
<parent>
@@ -19,11 +19,18 @@
1919
</dependency>
2020

2121
<dependency>
22-
<groupId>org.springframework.boot</groupId>
23-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
24-
</dependency>
25-
26-
27-
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
24+
</dependency>
2825
</dependencies>
26+
27+
<build>
28+
<finalName>spring-security-oauth-ui-implicit</finalName>
29+
<resources>
30+
<resource>
31+
<directory>src/main/resources</directory>
32+
<filtering>true</filtering>
33+
</resource>
34+
</resources>
35+
</build>
2936
</project>

spring-security-oauth/spring-security-oauth-ui/src/main/java/org/baeldung/config/UiApplication.java renamed to spring-security-oauth/spring-security-oauth-ui-implicit/src/main/java/org/baeldung/config/UiApplication.java

File renamed without changes.

spring-security-oauth/spring-security-oauth-ui/src/main/java/org/baeldung/config/UiWebConfig.java renamed to spring-security-oauth/spring-security-oauth-ui-implicit/src/main/java/org/baeldung/config/UiWebConfig.java

File renamed without changes.

0 commit comments

Comments
 (0)