-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pavelfomin/feature/spring-security-jwt
spring security jwt
- Loading branch information
Showing
11 changed files
with
325 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/droidablebee/springboot/rest/config/WebSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.droidablebee.springboot.rest.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.builders.WebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}") | ||
private String issuerUri; | ||
|
||
@Value("${app.security.ignore:/swagger/**, /swagger-resources/**, /swagger-ui.html, /webjars/**, /v2/api-docs, /actuator/info}") | ||
private String[] ignorePatterns; | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
|
||
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); | ||
|
||
http.csrf().disable(); // do not require SCRF for POST and PUT | ||
|
||
//make sure principal is created for the health endpoint to verify the role | ||
http.authorizeRequests().antMatchers("/actuator/health").permitAll(); | ||
|
||
http | ||
.authorizeRequests() | ||
// .mvcMatchers("/messages/**").hasAuthority("SCOPE_message:read") | ||
.anyRequest().authenticated() | ||
.and() | ||
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); | ||
} | ||
|
||
@Override | ||
public void configure(WebSecurity web) { | ||
|
||
web.ignoring().antMatchers(ignorePatterns); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
management: | ||
endpoints: | ||
web: | ||
exposure: | ||
include: info,health,env | ||
endpoint: | ||
health: | ||
show-details: when-authorized | ||
roles: SCOPE_health-details | ||
spring: | ||
jpa: | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
use_sql_comments: true | ||
jackson: | ||
# date-format: yyyy-MM-dd'T'HH:mm:ss.SSSZ: | ||
serialization: | ||
write_dates_as_timestamps: true | ||
security: | ||
oauth2: | ||
resourceserver: | ||
jwt: | ||
# this is to wire the "org.springframework.security.oauth2.jwt.JwtDecoder" bean correctly | ||
jwk-set-uri: http://localhost:9999/.well-known/jwks.json | ||
datasource: | ||
platform: h2 | ||
driverClassName: org.h2.Driver | ||
url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL;INIT=CREATE SCHEMA IF NOT EXISTS ddb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.