Skip to main content

AuthorizationManager Configuration

When using the OPA Spring Boot SDK in conjunction with a Spring Boot SDK system, the OPA path should be configured to main/main. The following code example demonstrates how to do this:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Autowired
TicketRepository ticketRepository;

@Autowired
TenantRepository tenantRepository;

@Autowired
CustomerRepository customerRepository;

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

String opaURL = "http://localhost:8181";
String opaURLEnv = System.getenv("OPA_URL");
if (opaURLEnv != null) {
opaURL = opaURLEnv;
}
OPAClient opa = new OPAClient(opaURL);

AuthorizationManager<RequestAuthorizationContext> am = new OPAAuthorizationManager(opa, "main/main");

// NOTE: The `.csrf(...)` disables CSRF protections. This could
// be a serious security vulnerability in a production environment.
// However, since this API is intended for educational and development
// purposes, it is disabled because it makes it easier to work with
// locally. If you want to use any of this code for a production
// service, it is important to re-enable CSRF protection.
http.authorizeHttpRequests(authorize -> authorize
.anyRequest().access(am)).csrf(csrf -> csrf.disable());

return http.build();
}


}

For further information, check out this guide.