Skip to content

Commit

Permalink
remove oauth extra resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Doha2012 committed Feb 23, 2016
1 parent 595c1c2 commit 1146126
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
.requestMatchers().antMatchers("/foos/**","/bars/**","/bazes/**")
.requestMatchers().antMatchers("/foos/**","/bars/**")
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET,"/foos/**").access("#oauth2.hasScope('foo') and #oauth2.hasScope('read')")
.antMatchers(HttpMethod.POST,"/foos/**").access("#oauth2.hasScope('foo') and #oauth2.hasScope('write')")
.antMatchers(HttpMethod.GET,"/bars/**").access("#oauth2.hasScope('bar') and #oauth2.hasScope('read')")
.antMatchers(HttpMethod.POST,"/bars/**").access("#oauth2.hasScope('bar') and #oauth2.hasScope('write')")
.antMatchers(HttpMethod.GET,"/bazes/**").access("#oauth2.hasScope('read') and hasRole('ROLE_ADMIN')")
.antMatchers(HttpMethod.POST,"/bazes/**").access("#oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
.antMatchers(HttpMethod.POST,"/bars/**").access("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
;
// @formatter:on
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Bar findById(@PathVariable final long id) {
}

// API - write
// @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write')")
// @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
@RequestMapping(method = RequestMethod.POST, value = "/bars")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,6 @@
});
}

// baz
$scope.baz = {id:0 , name:"sample baz"};
$scope.bazes = $resource("http://localhost:8081/spring-security-oauth-resource/bazes/:bazId",{bazId:'@id'});

$scope.getBaz = function(){
$scope.baz = $scope.bazes.get({bazId:$scope.baz.id});
}

$scope.createBaz = function(){
if($scope.baz.name.length==0)
{
$rootScope.message = "Baz name can not be empty";
return;
}
$scope.baz.id = null;
$scope.baz = $scope.bazes.save($scope.baz, function(){
$rootScope.message = "Baz Created Successfully";
});
}

});
/*]]>*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,6 @@ <h1>Bar Details</h1>
<a class="btn btn-default" href="#" ng-click="createBar()">Create Bar</a>
</div>
</div>
<br/>
<hr/>
<br/>
<br/>
<br/>
<h1>Baz Details</h1>
<div class="col-sm-6">
<div class="col-sm-12">
<label class="col-sm-2">ID</label>
<span class="col-sm-10"><input class="form-control" ng-model="baz.id"/></span>
</div>

<div class="col-sm-12">
<label class="col-sm-2">Name</label>
<span class="col-sm-10"><input class="form-control" ng-model="baz.name"/></span>
</div>

<div class="col-sm-12">
<a class="btn btn-default" href="#" ng-click="getBaz()">Get Baz</a>
<a class="btn btn-default" href="#" ng-click="createBaz()">Create Baz</a>
</div>
</div>


</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

import org.junit.Test;
import org.springframework.http.MediaType;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
Expand All @@ -33,56 +34,37 @@ public void givenUser_whenUseFooClient_thenOkForFooResourceOnly() {

final Response barResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bars/1");
assertEquals(403, barResponse.getStatusCode());

final Response bazResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bazes/1");
assertEquals(403, bazResponse.getStatusCode());
}

@Test
public void givenUser_whenUseBarClient_thenOkForBarResourceOnly() {
public void givenUser_whenUseBarClient_thenOkForBarResourceReadOnly() {
final String accessToken = obtainAccessToken("barClientIdPassword", "john", "123");

final Response barResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bars/1");
assertEquals(200, barResponse.getStatusCode());
assertNotNull(barResponse.jsonPath().get("name"));

final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/foos/1");
assertEquals(403, fooResponse.getStatusCode());

final Response bazResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bazes/1");
assertEquals(403, bazResponse.getStatusCode());
}
final Response barReadResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bars/1");
assertEquals(200, barReadResponse.getStatusCode());
assertNotNull(barReadResponse.jsonPath().get("name"));

@Test
public void givenAdmin_whenUseFooClient_thenOkForFooAndBazResourceOnly() {
final String accessToken = obtainAccessToken("fooClientIdPassword", "tom", "111");

final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/foos/1");
assertEquals(200, fooResponse.getStatusCode());
assertNotNull(fooResponse.jsonPath().get("name"));

final Response bazResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bazes/1");
assertEquals(200, bazResponse.getStatusCode());
assertNotNull(bazResponse.jsonPath().get("name"));

final Response barResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bars/1");
assertEquals(403, barResponse.getStatusCode());
final Response barWritResponse = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken).body("{\"id\":1,\"name\":\"MyBar\"}").post("http://localhost:8081/spring-security-oauth-resource/bars");
assertEquals(403, barWritResponse.getStatusCode());
}

@Test
public void givenAdmin_whenUseBarClient_thenOkForBarAndBazResourceOnly() {
public void givenAdmin_whenUseBarClient_thenOkForBarResourceReadWrite() {
final String accessToken = obtainAccessToken("barClientIdPassword", "tom", "111");

final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/foos/1");
assertEquals(403, fooResponse.getStatusCode());

final Response barResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bars/1");
assertEquals(200, barResponse.getStatusCode());
assertNotNull(barResponse.jsonPath().get("name"));

final Response bazResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/bazes/1");
assertEquals(200, bazResponse.getStatusCode());
assertNotNull(bazResponse.jsonPath().get("name"));

final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8081/spring-security-oauth-resource/foos/1");
assertEquals(403, fooResponse.getStatusCode());
final Response barWritResponse = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken).body("{\"id\":1,\"name\":\"MyBar\"}").post("http://localhost:8081/spring-security-oauth-resource/bars");
assertEquals(201, barWritResponse.getStatusCode());
assertEquals("MyBar", barWritResponse.jsonPath().get("name"));
}

}

0 comments on commit 1146126

Please sign in to comment.