Skip to content

Commit 1a20922

Browse files
authored
Merge pull request #1 from skumar3006/develop
Develop
2 parents df7353a + 0db337b commit 1a20922

File tree

8 files changed

+290
-30
lines changed

8 files changed

+290
-30
lines changed

pom.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
<groupId>org.projectlombok</groupId>
3838
<artifactId>lombok</artifactId>
3939
</dependency>
40-
<dependency>
41-
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-starter-data-rest</artifactId>
43-
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-rest</artifactId>
43+
</dependency>
4444
<dependency>
4545
<groupId>org.flywaydb</groupId>
4646
<artifactId>flyway-core</artifactId>
@@ -61,6 +61,20 @@
6161
<artifactId>spring-boot-starter-test</artifactId>
6262
<scope>test</scope>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-security</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework.security.oauth</groupId>
70+
<artifactId>spring-security-oauth2</artifactId>
71+
<version>2.3.3.RELEASE</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.modelmapper</groupId>
75+
<artifactId>modelmapper</artifactId>
76+
<version>2.3.5</version>
77+
</dependency>
6478
</dependencies>
6579

6680
</project>
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package sandeep.demo.controller;
22

33
import java.util.List;
4-
import java.util.Optional;
54

65
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.http.HttpHeaders;
86
import org.springframework.http.HttpStatus;
9-
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.DeleteMapping;
108
import org.springframework.web.bind.annotation.GetMapping;
119
import org.springframework.web.bind.annotation.PathVariable;
1210
import org.springframework.web.bind.annotation.PostMapping;
@@ -15,8 +13,8 @@
1513
import org.springframework.web.bind.annotation.RequestMapping;
1614
import org.springframework.web.bind.annotation.RestController;
1715

18-
import sandeep.demo.entity.Owner;
1916
import sandeep.demo.manager.OwnerManager;
17+
import sandeep.demo.model.OwnerBO;
2018

2119
@RestController
2220
@RequestMapping("/owners")
@@ -26,28 +24,34 @@ public class OwnerController {
2624
OwnerManager ownerManager;
2725

2826
@GetMapping
29-
public ResponseEntity<List<Owner>> getAllRecords() {
30-
List<Owner> list = ownerManager.getAllRecords();
31-
32-
return new ResponseEntity<List<Owner>>(list, new HttpHeaders(), HttpStatus.OK);
27+
public List<OwnerBO> getAllRecords() {
28+
List<OwnerBO> list = ownerManager.getAllRecords();
29+
return list;
3330
}
3431

3532
@GetMapping(value = "/{ownerId}")
36-
public Optional<Owner> getById(@PathVariable Integer ownerId) {
37-
Optional<Owner> owner = ownerManager.getById(ownerId);
33+
public OwnerBO getById(@PathVariable Integer ownerId) {
34+
OwnerBO owner = ownerManager.getById(ownerId);
3835
return owner;
3936
}
4037

4138
@PostMapping
42-
public ResponseEntity<Owner> createOrUpdateEmployee(@RequestBody Owner owner) {
43-
Owner response = ownerManager.createOwner(owner);
44-
return new ResponseEntity<Owner>(response, new HttpHeaders(), HttpStatus.OK);
39+
public OwnerBO createOrUpdateEmployee(@RequestBody OwnerBO owner) {
40+
OwnerBO ownerBO = ownerManager.createOwner(owner);
41+
return ownerBO;
4542
}
4643

4744
@PutMapping(value = "{/ownerId}")
48-
public ResponseEntity<Owner> update(Owner owner) {
49-
Owner response = ownerManager.updateOwner(owner);
50-
return new ResponseEntity<Owner>(response, new HttpHeaders(), HttpStatus.OK);
45+
public OwnerBO update(@RequestBody OwnerBO owner) {
46+
OwnerBO ownerBO = ownerManager.updateOwner(owner);
47+
return ownerBO;
48+
}
49+
50+
@DeleteMapping(value = "/{ownerId}")
51+
public HttpStatus deleteById(@PathVariable Integer ownerId) {
52+
ownerManager.deleteById(ownerId);
53+
return HttpStatus.ACCEPTED;
5154
}
5255

56+
5357
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package sandeep.demo.manager;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.Optional;
56

67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Component;
89

910
import sandeep.demo.entity.Owner;
11+
import sandeep.demo.model.ModelEntityMapper;
12+
import sandeep.demo.model.OwnerBO;
1013
import sandeep.demo.repository.OwnerRepository;
1114

1215
@Component
@@ -15,22 +18,32 @@ public class OwnerManager {
1518
@Autowired
1619
OwnerRepository ownerRepository;
1720

18-
public Owner createOwner(Owner owner) {
19-
ownerRepository.save(owner);
20-
return owner;
21+
public OwnerBO createOwner(OwnerBO ownerBO) {
22+
Owner owner = ModelEntityMapper.convertToEntity(ownerBO);
23+
owner = ownerRepository.save(owner);
24+
return ModelEntityMapper.convertToBO(owner);
2125
}
2226

23-
public List<Owner> getAllRecords() {
24-
return (List<Owner>) ownerRepository.findAll();
27+
public List<OwnerBO> getAllRecords() {
28+
List<Owner> owners = (List<Owner>) ownerRepository.findAll();
29+
List<OwnerBO> list = new ArrayList<>();
30+
owners.forEach(owner -> list.add(ModelEntityMapper.convertToBO(owner)));
31+
return list;
2532
}
2633

27-
public Owner updateOwner(Owner owner) {
28-
ownerRepository.save(owner);
29-
return owner;
34+
public OwnerBO updateOwner(OwnerBO ownerBO) {
35+
Owner owner = ModelEntityMapper.convertToEntity(ownerBO);
36+
owner = ownerRepository.save(owner);
37+
return ModelEntityMapper.convertToBO(owner);
3038
}
3139

32-
public Optional<Owner> getById(Integer ownerId) {
33-
return ownerRepository.findById(ownerId);
40+
public OwnerBO getById(Integer ownerId) {
41+
Optional<Owner> owner = ownerRepository.findById(ownerId);
42+
return ModelEntityMapper.convertToBO(owner.get());
43+
}
44+
45+
public void deleteById(Integer ownerId) {
46+
ownerRepository.deleteById(ownerId);
3447
}
3548

3649
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sandeep.demo.model;
2+
3+
import org.modelmapper.ModelMapper;
4+
5+
import sandeep.demo.entity.Owner;
6+
7+
public class ModelEntityMapper {
8+
9+
public static OwnerBO convertToBO(Owner owner) {
10+
11+
ModelMapper modelMapper = new ModelMapper();
12+
OwnerBO ownerBO = modelMapper.map(owner, OwnerBO.class);
13+
return ownerBO;
14+
15+
}
16+
17+
public static Owner convertToEntity(OwnerBO ownerBO) {
18+
ModelMapper modelMapper = new ModelMapper();
19+
Owner owner = modelMapper.map(ownerBO, Owner.class);
20+
return owner;
21+
22+
}
23+
24+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package sandeep.demo.spring.security;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.security.authentication.AuthenticationManager;
8+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
9+
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
10+
import org.springframework.security.crypto.password.PasswordEncoder;
11+
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
12+
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
13+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
14+
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
15+
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
16+
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
17+
import org.springframework.security.oauth2.provider.token.TokenStore;
18+
19+
/**
20+
*
21+
* @author Sandeep Kumar Vishwakarma
22+
* Authorization Server
23+
*/
24+
25+
@Configuration
26+
@EnableAuthorizationServer
27+
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
28+
29+
private static String REALM = "MY_OAUTH_REALM";
30+
31+
@Autowired
32+
private TokenStore tokenStore;
33+
34+
@Autowired
35+
private UserApprovalHandler userApprovalHandler;
36+
37+
@Autowired
38+
@Qualifier("authenticationManagerBean")
39+
private AuthenticationManager authenticationManager;
40+
41+
@Override
42+
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
43+
44+
clients.inMemory().withClient("sandeep")
45+
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
46+
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT").scopes("read", "write", "trust").secret("{noop}secret")
47+
.accessTokenValiditySeconds(120).// Access token is only valid for 2 minutes.
48+
refreshTokenValiditySeconds(600);// Refresh token is only valid for 10 minutes.
49+
}
50+
51+
@Override
52+
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
53+
endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
54+
.authenticationManager(authenticationManager);
55+
}
56+
57+
58+
59+
@Bean
60+
public PasswordEncoder passwordEncoder() {
61+
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
62+
}
63+
64+
@Override
65+
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
66+
oauthServer.realm(REALM + "/client");
67+
}
68+
69+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sandeep.demo.spring.security;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
5+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
6+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
7+
import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;
8+
9+
/**
10+
*
11+
* @author Sandeep Kumar Vishwakarma
12+
* Enable Global method security which will activate @PreFilter, @PostFilter, @PreAuthorize @PostAuthorize
13+
*
14+
*/
15+
16+
17+
@Configuration
18+
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
19+
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
20+
21+
@Override
22+
protected MethodSecurityExpressionHandler createExpressionHandler() {
23+
return new OAuth2MethodSecurityExpressionHandler();
24+
}
25+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package sandeep.demo.spring.security;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.authentication.AuthenticationManager;
7+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
8+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
11+
import org.springframework.security.oauth2.provider.ClientDetailsService;
12+
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
13+
import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
14+
import org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler;
15+
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
16+
import org.springframework.security.oauth2.provider.token.TokenStore;
17+
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
18+
19+
/**
20+
*
21+
* @author Sandeep Kumar Vishwakarma Security Configuration
22+
*
23+
*/
24+
25+
@Configuration
26+
@EnableWebSecurity
27+
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
28+
29+
@Autowired
30+
private ClientDetailsService clientDetailsService;
31+
32+
@Autowired
33+
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
34+
auth.inMemoryAuthentication().withUser("sandeep").password("{noop}sandeep123").roles("ADMIN").and().withUser("user")
35+
.password("{noop}user123").roles("USER");
36+
}
37+
38+
@Override
39+
protected void configure(HttpSecurity http) throws Exception {
40+
http.csrf().disable().anonymous().disable().authorizeRequests().antMatchers("/oauth/token").permitAll();
41+
}
42+
43+
@Override
44+
@Bean
45+
public AuthenticationManager authenticationManagerBean() throws Exception {
46+
return super.authenticationManagerBean();
47+
}
48+
49+
@Bean
50+
public TokenStore tokenStore() {
51+
return new InMemoryTokenStore();
52+
}
53+
54+
@Bean
55+
@Autowired
56+
public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore) {
57+
TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
58+
handler.setTokenStore(tokenStore);
59+
handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
60+
handler.setClientDetailsService(clientDetailsService);
61+
return handler;
62+
}
63+
64+
@Bean
65+
@Autowired
66+
public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
67+
TokenApprovalStore store = new TokenApprovalStore();
68+
store.setTokenStore(tokenStore);
69+
return store;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)