-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($API-Portal): add register and login
- Loading branch information
1 parent
45dd94f
commit e4e0318
Showing
10 changed files
with
268 additions
and
16 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...rtal/src/main/java/com/jmsoftware/apiportal/auth/controller/AuthenticationController.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 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)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
api-portal/src/main/java/com/jmsoftware/apiportal/auth/entity/LoginPayload.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,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; | ||
} |
16 changes: 16 additions & 0 deletions
16
api-portal/src/main/java/com/jmsoftware/apiportal/auth/entity/LoginResponse.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,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; | ||
} |
37 changes: 37 additions & 0 deletions
37
api-portal/src/main/java/com/jmsoftware/apiportal/auth/entity/RegisterPayload.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,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; | ||
} |
30 changes: 30 additions & 0 deletions
30
api-portal/src/main/java/com/jmsoftware/apiportal/auth/service/AuthenticationService.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,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); | ||
} |
55 changes: 55 additions & 0 deletions
55
...l/src/main/java/com/jmsoftware/apiportal/auth/service/impl/AuthenticationServiceImpl.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,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; | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -54,7 +54,7 @@ public interface UserService { | |
* Search user by username | ||
* | ||
* @param username username | ||
* @return user | ||
* @return user user po | ||
*/ | ||
UserPO searchUserByUsername(String username); | ||
|
||
|
@@ -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); | ||
} |
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
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