-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in #9_Add_spring_security (pull request #12)
#9 Add spring security * #9 copy spring security files from my_stuff * #9 changing tests * git ignore * #9 delete Device * #9 AuthDetails now implements UserDetails * #9 change userdetails service to auth details service * #9 add model mapper & fixsome test * #9 merge * #9 modelMapper modification * #9 rename webConfig to ModelMapperConfig * #9 small changes * #9 rename userDto to AuthDetails * #9 add serialVersionUuid to user * #9 cleaning * #9 test fixes Approved-by: Maxim Levitskiy <[email protected]>
- Loading branch information
1 parent
82bb4e8
commit e8e4d2b
Showing
30 changed files
with
1,148 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
*.iml | ||
|
||
# System Files | ||
.DS_Store | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
back/src/main/java/org/webtree/trust/advice/SecurityControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.webtree.trust.advice; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.InternalAuthenticationServiceException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Created by Udjin on 21.03.2018. | ||
*/ | ||
@ControllerAdvice(annotations = RestController.class) | ||
public class SecurityControllerAdvice { | ||
|
||
private MessageSource messageSource; | ||
private static final String LOGIN_ERROR = "login.badCredentials"; | ||
|
||
@Autowired | ||
public SecurityControllerAdvice(MessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
@ExceptionHandler(InternalAuthenticationServiceException.class) | ||
public ResponseEntity<String> badUserNameHandler() { | ||
return createError(LOGIN_ERROR); | ||
} | ||
|
||
@ExceptionHandler(BadCredentialsException.class) | ||
public ResponseEntity<String> badPasswordHandler() { | ||
return createError(LOGIN_ERROR); | ||
} | ||
|
||
private ResponseEntity<String> createError(String errorCode) { | ||
String errorMessage = messageSource.getMessage(errorCode, new Object[]{}, Locale.getDefault()); | ||
return new ResponseEntity<>(errorMessage, new HttpHeaders(), HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
back/src/main/java/org/webtree/trust/common/utils/TimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.webtree.trust.common.utils; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
|
||
@Component | ||
public class TimeProvider { | ||
public Date now() { | ||
return new Date(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
back/src/main/java/org/webtree/trust/config/ModelMapperConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.webtree.trust.config; | ||
|
||
|
||
import org.modelmapper.ModelMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import org.webtree.trust.domain.User; | ||
import org.webtree.trust.domain.AuthDetals; | ||
|
||
|
||
@ComponentScan("org.webtree.trust") | ||
@Configuration() | ||
public class ModelMapperConfig { | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@Bean | ||
public ModelMapper mapper() { | ||
ModelMapper modelMapper = new ModelMapper(); | ||
/* modelMapper.addConverter(new UserDTOToUserConverter(passwordEncoder()));*/ | ||
modelMapper.createTypeMap(AuthDetals.class, User.class).addMappings( | ||
mapper -> | ||
mapper | ||
.using(ctx -> { | ||
String encodedPass = ctx.getSource().toString(); | ||
return passwordEncoder.encode(encodedPass); | ||
}) | ||
.map(AuthDetals::getPassword, User::setPassword) | ||
); | ||
return modelMapper; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
back/src/main/java/org/webtree/trust/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.webtree.trust.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
|
||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.webtree.trust.security.JwtAuthenticationEntryPoint; | ||
import org.webtree.trust.security.JwtAuthenticationTokenFilter; | ||
import org.webtree.trust.security.JwtTokenUtil; | ||
import org.webtree.trust.service.UserService; | ||
|
||
@ComponentScan("org.webtree.trust") | ||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
private final UserService userService; | ||
private final JwtTokenUtil tokenUtil; | ||
private final JwtAuthenticationEntryPoint unauthorizedHandler; | ||
|
||
@Autowired | ||
public SecurityConfig(UserService userService, JwtTokenUtil tokenUtil, JwtAuthenticationEntryPoint unauthorizedHandler) { | ||
this.userService = userService; | ||
this.tokenUtil = tokenUtil; | ||
this.unauthorizedHandler = unauthorizedHandler; | ||
} | ||
|
||
@Autowired | ||
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder, UserService userService) throws Exception { | ||
|
||
authenticationManagerBuilder | ||
.userDetailsService(userService) | ||
.passwordEncoder(passwordEncoder()); | ||
|
||
} | ||
|
||
@Bean | ||
@Override | ||
public AuthenticationManager authenticationManagerBean() throws Exception { | ||
return super.authenticationManagerBean(); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception { | ||
return new JwtAuthenticationTokenFilter(userService, tokenUtil); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity httpSecurity) throws Exception { | ||
httpSecurity | ||
// we don't need CSRF because our token is invulnerable | ||
.csrf().disable() | ||
.cors().and() | ||
|
||
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() | ||
|
||
// don't create session | ||
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() | ||
|
||
.authorizeRequests() | ||
//.antMatchers(HttpMethod.OPTIONS, "/**").permitAll() | ||
|
||
.antMatchers("/rest/token/new", "/rest/user/register").permitAll() | ||
.anyRequest().authenticated(); | ||
|
||
// Custom JWT based security filter | ||
httpSecurity | ||
.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); | ||
|
||
// disable page caching | ||
httpSecurity.headers().cacheControl(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
back/src/main/java/org/webtree/trust/controller/AbstractController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.webtree.trust.controller; | ||
|
||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
|
||
@CrossOrigin(origins = "${frontend.origins}") | ||
public class AbstractController { | ||
} |
63 changes: 63 additions & 0 deletions
63
back/src/main/java/org/webtree/trust/controller/SecurityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.webtree.trust.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.webtree.trust.domain.AuthDetals; | ||
import org.webtree.trust.domain.User; | ||
import org.webtree.trust.security.JwtTokenUtil; | ||
import org.webtree.trust.service.UserService; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
@RestController | ||
@RequestMapping("/rest") | ||
public class SecurityController extends AbstractController { | ||
|
||
private final AuthenticationManager authenticationManager; | ||
|
||
@Value("${jwt.header}") | ||
private String tokenHeader; | ||
|
||
private JwtTokenUtil jwtTokenUtil; | ||
private UserService userService; | ||
|
||
@Autowired | ||
public SecurityController(AuthenticationManager authenticationManager, JwtTokenUtil jwtTokenUtil, UserService userService) { | ||
this.authenticationManager = authenticationManager; | ||
this.jwtTokenUtil = jwtTokenUtil; | ||
this.userService = userService; | ||
} | ||
|
||
@PostMapping("${jwt.route.authentication.path}") | ||
public ResponseEntity<?> login(@RequestBody AuthDetals authDetals/*, Device device*/) { | ||
Authentication authentication = | ||
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken( | ||
authDetals.getUsername(), authDetals.getPassword())); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
User user = userService.loadUserByUsername(authDetals.getUsername()); | ||
return ResponseEntity.ok(jwtTokenUtil.generateToken(user/*, device)*/)); | ||
} | ||
|
||
@PostMapping | ||
@RequestMapping(value = "${jwt.route.authentication.refresh}", method = RequestMethod.GET) | ||
public ResponseEntity<?> refreshAndGetAuthenticationToken(HttpServletRequest request) { | ||
String token = request.getHeader(tokenHeader); | ||
String username = jwtTokenUtil.getUsernameFromToken(token); | ||
User user = userService.loadUserByUsername(username); | ||
|
||
if (jwtTokenUtil.canTokenBeRefreshed(token, user.getLastPasswordResetDate())) { | ||
String refreshedToken = jwtTokenUtil.refreshToken(token); | ||
return ResponseEntity.ok(refreshedToken); | ||
} else { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
} | ||
} |
14 changes: 10 additions & 4 deletions
14
back/src/main/java/org/webtree/trust/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
back/src/main/java/org/webtree/trust/controller/alfa/TrustController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.