Posts

Showing posts with the label Restful

get file from message - JavaMail library

get bytes BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) obj; byte[] byteArray = IOUtils.toByteArray(base64DecoderStream); byte[] encodeBase64 = Base64.encodeBase64(byteArray); ========= return inputstream for download process Resource <= InputStreamResource return new InputStreamResource(part.getInputStream());

send request with body - RestTemplate

HttpHeaders headers = new HttpHeaders();  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();  data.add("client_id",clientId);  data.add("client_secret",clientSecret);  data.add("grant_type",grantType);  data.add("redirect_uri",redirectUrl);  data.add("code",code.split("#")[0]);  HttpEntity<MultiValueMap<String, String>>  request = new HttpEntity<>(data, headers);  ResponseEntity<String> response = restTemplate.postForEntity(accessTokenUrl,request,String.class);

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 );

Restful API Security

Image
pom.xml Tutaq ki, app /movies api a muraciet edir. Bu zaman, o ilk once OAuth2 serverine chatir. Url e muraciet etdiyi zaman, ozunde appId , secretKey, username, password kimi melumatlari saxladir. OAuth2 serverine chatdiqda, appId ve secretKey e gore muracietin mumkunluyunu teyin edir. Meselen app in /movies e muraciet huququ var , lakin /news a muraciet huququ yoxdur. Eger muraciet huququ yoxdursa, hech restful a muraciet gedib chatmir, yari yolda OAuth2 terefinden saxladilir. Tutaq ki , muraciet huququ var, bu zaman restful a muraciet olunur. Muraciet zamani gelen username ve password burada rol oynayir. Restful API teyin edir ki, bu username ve password var mi , varsa hansi huquqlara malikdir. Alinan neticeni geri qaytarir. BasicAuthentication ve DigestAuthentication BasicAuth da api e her defe sorgu gonderende username ve password gonderilir ve bu melumatlara gore huququ teyin edilir, hech bir token olmur. Bu melumatlar gonderilen zaman hashlanmamish formada gonderili...