Skip to content

Instantly share code, notes, and snippets.

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests(authorizeRequests ->
authorizeRequests
.antMatchers(HttpMethod.GET, "/hello").hasAuthority("SCOPE_hello")
.anyRequest().authenticated()
@RestController
public class HelloController {
@GetMapping("/hello")
public ResponseEntity<String> hello() {
return ResponseEntity.ok().body("hello");
}
}
@pedroveras
pedroveras / application.yml
Created December 4, 2020 03:02
Configuring Spring Security
## spring security resource server configuration
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: ${KEYCLOAK_ISSUER_URI:http://localhost:18080/auth/realms/hello}
jwk-set-uri: ${KEYCLOAK_JWKS_URI:http://localhost:18080/auth/realms/hello/protocol/openid-connect/certs}
version: '3'
services:
postgres:
image: 'postgres:alpine'
volumes:
- postgres-volume:/var/lib/postgresql/data
ports:
- 5432:5432
environment:
@AllArgsConstructor
public class MonthlyExpensesRule implements Rule {
private Rule nextRule;
@Override
public boolean validate(Client client) {
BigDecimal expensesAmount = client.getExpenses().stream().filter(this::validatePeriod)
.map(Expense::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
@AllArgsConstructor
public class TimeAsClientRule implements Rule {
@Override
public boolean validate(Client client) {
return client.getClientSince().plusYears(CashbackParams.TIME_AS_CLIENT)
.isBefore(LocalDateTime.now());
}
}
@AllArgsConstructor
public class SegmentRule implements Rule {
private Rule nextRule;
@Override
public boolean validate(Client client) {
if (client.getSegment().equals(CashbackParams.SEGMENT)) {
return nextRule.validate(client);
}
public interface Rule {
void validate(Client client);
}