Skip to content

Commit

Permalink
feat($API-Portal): add register and login
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed May 9, 2020
1 parent 45dd94f commit e4e0318
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.jmsoftware.apiportal.auth.controller;

import com.jmsoftware.apiportal.auth.entity.LoginPayload;
import com.jmsoftware.apiportal.auth.entity.LoginResponse;
import com.jmsoftware.apiportal.auth.entity.RegisterPayload;
import com.jmsoftware.apiportal.auth.service.AuthenticationService;
import com.jmsoftware.common.bean.ResponseBodyBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* <h1>AuthenticationController</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 5/8/20 11:06 AM
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/authentication")
@Api(tags = {"Authentication Controller"})
public class AuthenticationController {
private final AuthenticationService authenticationService;

@PostMapping("/register")
@ApiOperation(value = "/register", notes = "Register (create an account)")
public ResponseBodyBean<Object> register(@Valid @RequestBody RegisterPayload payload) {
authenticationService.register(payload);
return ResponseBodyBean.ofSuccess();
}

@PostMapping("/login")
@ApiOperation(value = "/login", notes = "Login")
public ResponseBodyBean<LoginResponse> login(@Valid @RequestBody LoginPayload payload) {
return ResponseBodyBean.ofSuccess(authenticationService.login(payload));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jmsoftware.apiportal.auth.entity;

import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
* <h1>LoginPayload</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 5/8/20 2:08 PM
**/
@Data
public class LoginPayload {
/**
* The Login token: username / email
*/
@NotEmpty
@Length(max = 100)
private String loginToken;
/**
* The Password.
*/
@NotEmpty
@Length(max = 60)
private String password;
/**
* Remember me
*/
@NotNull
private Boolean rememberMe;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jmsoftware.apiportal.auth.entity;

import lombok.Data;

/**
* <h1>LoginResponse</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 5/8/20 2:07 PM
**/
@Data
public class LoginResponse {
private String jwt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.jmsoftware.apiportal.auth.entity;

import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;

/**
* <h1>RegisterPayload</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 5/9/20 3:53 PM
**/
@Data
public class RegisterPayload {
/**
* Username (Unique)
*/
@NotEmpty
@Length(min = 4, max = 50)
private String username;
/**
* Email (Unique)
*/
@NotEmpty
@Size(max = 30)
private String email;
/**
* Password
*/
@NotEmpty
@Length(min = 8, max = 30)
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jmsoftware.apiportal.auth.service;

import com.jmsoftware.apiportal.auth.entity.LoginPayload;
import com.jmsoftware.apiportal.auth.entity.LoginResponse;
import com.jmsoftware.apiportal.auth.entity.RegisterPayload;

/**
* <h1>AuthenticationService</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 5/8/20 2:04 PM
*/
public interface AuthenticationService {
/**
* Register.
*
* @param payload the payload
*/
void register(RegisterPayload payload);

/**
* Login login response.
*
* @param payload the payload
* @return the login response
*/
LoginResponse login(LoginPayload payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jmsoftware.apiportal.auth.service.impl;

import com.jmsoftware.apiportal.auth.entity.LoginPayload;
import com.jmsoftware.apiportal.auth.entity.LoginResponse;
import com.jmsoftware.apiportal.auth.entity.RegisterPayload;
import com.jmsoftware.apiportal.auth.service.AuthenticationService;
import com.jmsoftware.apiportal.universal.domain.UserPO;
import com.jmsoftware.apiportal.universal.service.JwtService;
import com.jmsoftware.apiportal.universal.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
* <h1>AuthenticationServiceImpl</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 5/8/20 2:04 PM
**/
@Service
@RequiredArgsConstructor
public class AuthenticationServiceImpl implements AuthenticationService {
private final BCryptPasswordEncoder encoder;
private final AuthenticationManager authenticationManager;
private final JwtService jwtService;
private final UserService userService;

@Override
public void register(RegisterPayload payload) {
val userPo = new UserPO();
userPo.setUsername(payload.getUsername());
userPo.setEmail(payload.getEmail());
userPo.setPassword(encoder.encode(payload.getPassword()));
// TODO: user service should be in the `auth-center`
userService.saveUser(userPo);
}

@Override
public LoginResponse login(LoginPayload payload) {
val authenticationToken = new UsernamePasswordAuthenticationToken(payload.getLoginToken(),
payload.getPassword());
val authentication = authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
val jwt = jwtService.createJwt(authentication, payload.getRememberMe());
val loginResponse = new LoginResponse();
loginResponse.setJwt(jwt);
return loginResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -117,6 +118,16 @@ public ResponseBodyBean<Object> handleException(HttpServletRequest request,
log.error("[GlobalExceptionCapture]: Exception information: {} ", exception.getMessage());
response.setStatus(HttpStatus.BAD_CREDENTIALS.getCode());
return ResponseBodyBean.ofStatus(HttpStatus.BAD_CREDENTIALS.getCode(), exception.getMessage(), null);
} else if (exception instanceof InternalAuthenticationServiceException) {
log.error("[GlobalExceptionCapture]: Exception information: {} ", exception.getMessage());
if (exception.getCause() instanceof BaseException) {
val exceptionCause = (BaseException) exception.getCause();
val code = exceptionCause.getCode();
response.setStatus(code);
return ResponseBodyBean.ofStatus(HttpStatus.fromCode(code));
}
response.setStatus(HttpStatus.ERROR.getCode());
return ResponseBodyBean.ofStatus(HttpStatus.BAD_CREDENTIALS.getCode(), exception.getMessage(), null);
}
log.error("[GlobalExceptionCapture]: Exception information: {} ", exception.getMessage(), exception);
response.setStatus(HttpStatus.ERROR.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* <p>Change description here</p>
*
* @author Johnny Miller (鍾俊), email: [email protected]
* @date 2019-06-07 11:42
**/
* @date 2019 -06-07 11:42
*/
public interface UserService {
/**
* Get user page list
Expand Down Expand Up @@ -54,7 +54,7 @@ public interface UserService {
* Search user by username
*
* @param username username
* @return user
* @return user user po
*/
UserPO searchUserByUsername(String username);

Expand Down Expand Up @@ -103,8 +103,15 @@ public interface UserService {
* Get user's avatar resource
*
* @param username username
* @return user's avatar
* @return user 's avatar
* @throws IOException IO exception
*/
ByteArrayResource getUserAvatarResource(String username) throws IOException;

/**
* Save user.
*
* @param userPo the user po
*/
void saveUser(UserPO userPo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ public ByteArrayResource getUserAvatarResource(String username) throws IOExcepti
@Cleanup InputStream stream = sftpService.read(po.getAvatar());
return new ByteArrayResource(stream.readAllBytes());
}

@Override
public void saveUser(UserPO userPo) {
userMapper.register(userPo);
}
}
34 changes: 22 additions & 12 deletions api-portal/src/main/resources/mapper/rbac/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@

<insert id="save" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user(username,
email,
cellphone,
password,
full_name,
birthday,
gender,
created_time,
modified_time,
status)
email,
cellphone,
password,
full_name,
birthday,
gender,
created_time,
modified_time,
status)
VALUES (#{username},
#{password},
#{email},
#{cellphone},
#{password},
#{fullName},
Expand All @@ -89,8 +89,18 @@
</insert>

<insert id="register" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user(username, password, email, created_time, modified_time, status)
VALUES (#{username}, #{password}, #{email}, now(), now(), 1)
INSERT INTO user(username,
password,
email,
created_time,
modified_time,
status)
VALUES (#{username},
#{password},
#{email},
now(),
now(),
1)
</insert>

<select id="selectUserPageList" resultMap="BaseResultMap">
Expand Down

0 comments on commit e4e0318

Please sign in to comment.