Posts

Showing posts with the label Security

Spring Boot / Embedded Tomcat - SSL

 @Bean     public ServletWebServerFactory servletContainer() {         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {             @Override             protected void postProcessContext(Context context) {                 var securityConstraint = new SecurityConstraint();                 securityConstraint.setUserConstraint("CONFIDENTIAL");                 var collection = new SecurityCollection();                 collection.addPattern("/*");                 securityConstraint.addCollection(collection);                 context.addConstraint(securityConstraint);             }       ...

Stateful vs Stateless

Image
 

JWT - generate public and private key (YML example)

@SpringBootApplication public class AuthServerApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(AuthServerApplication.class, args); } @Override public void run(String... args) throws Exception { KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA"); keyGenerator.initialize(1024); KeyPair kp = keyGenerator.genKeyPair(); PublicKey publicKey = kp.getPublic(); PrivateKey privateKey = kp.getPrivate(); String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded()); String encodedPrivateKey = Base64.getEncoder().encodeToString(privateKey.getEncoded()); System.out.println(convertToPublicKey(encodedPublicKey)); System.out.println(); System.out.println(convertToPrivateKey(encodedPrivateKey)); } private static String convertToPrivateKey(String key) {         StringBuilder result = new StringBuilder();         result.append(...

oauth/token request manual

ResponseEntity<String> response = null; RestTemplate restTemplate = new RestTemplate(); String credentials = "clientId:clientSecret"; String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes())); //        System.out.println(username); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("Authorization", "Basic " + encodedCredentials); HttpEntity<String> request = new HttpEntity<String>(headers); String accessTokenUrl = "http://localhost:8080/api/oauth/token" + "?grant_type=password" +         "&username=username" +         "&password=password" +         "&scope=read write"; response = restTemplate.exchange(accessTokenUrl, HttpMethod.POST, request, String.class); //        System.out.println(response.getBody().split(",...

oauth_access_token table

create table oauth_access_token ( token_id VARCHAR(255), token BLOB, authentication_id VARCHAR(255), user_name VARCHAR(255), client_id VARCHAR(255), authentication BLOB, refresh_token VARCHAR(255) ); create table oauth_refresh_token ( token_id VARCHAR(255), token BLOB, authentication BLOB );