Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurando o Resource Server com a nova stack do Spring Security #338

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jpb/jpb-settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<persistence-units>
<persistence-unit moduleName="algafood" name="Default">
<packages>
<package value="com.algaworks.algafood" />
<package value="com.tallyto.algafood" />
</packages>
</persistence-unit>
</persistence-units>
Expand Down
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@
<version>2.2.224</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>

<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>oauth2-oidc-sdk</artifactId>
<version>6.21.2</version>
</dependency>
</dependencies>


Expand Down Expand Up @@ -237,6 +248,15 @@
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>central</id>
<name>Maven Central</name>
<layout>default</layout>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

@Component
public class CidadeModelAssembler extends RepresentationModelAssemblerSupport<Cidade, CidadeModel> {
@Autowired
ModelMapper modelMapper;
private final ModelMapper modelMapper;

@Autowired
LinkBuilder linkBuilder;
private final LinkBuilder linkBuilder;

public CidadeModelAssembler() {
@Autowired
public CidadeModelAssembler(ModelMapper modelMapper, LinkBuilder linkBuilder) {
super(CidadeController.class, CidadeModel.class);

this.linkBuilder = linkBuilder;
this.modelMapper = modelMapper;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,30 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector;
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

@Bean
public static PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public UserDetailsService userDetailsService() {

UserDetails peter = User.builder().username("tallyto").password(passwordEncoder().encode("tallyto@123")).roles("USER")
.build();

UserDetails admin = User.builder().username("admin").password(passwordEncoder().encode("admin")).roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(peter,admin);
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.authorizeHttpRequests()
.antMatchers("/v1/cozinhas/**").permitAll()
http.authorizeHttpRequests()
.anyRequest().authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
.oauth2ResourceServer().opaqueToken();

return http.build();
}


@Bean
public OpaqueTokenIntrospector introspector() {
return new NimbusOpaqueTokenIntrospector("http://localhost:3002/oauth/check_token", "algafood-web", "web123");
}

}
3 changes: 3 additions & 0 deletions src/main/resources/application-developer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ spring.mvc.pathmatch.matching-strategy=ant-path-matcher
server.compression.enabled=true
logging.loggly.token=${LOGGLY_TOKEN}

spring.security.oauth2.resourceserver.opaquetoken.introspection-uri=http://localhost:3002/oauth/check_token
spring.security.oauth2.resourceserver.opaquetoken.client-id=checktoken
spring.security.oauth2.resourceserver.opaquetoken.client-secret=check123
Loading