Skip to content

Commit

Permalink
feat($AuthCenter): expose login API
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Miller (锺俊) committed Dec 22, 2020
1 parent 9d7e759 commit fe3f211
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ maf:
configuration:
ignored-url:
post:
- "/authentication/**"
- "/auth-center/users/login"
get:
- "/favicon.ico"
- "/auth/check-username-uniqueness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public AuthenticationManager authenticationManager() throws Exception {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Disable Web Security.
http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
http.authorizeRequests().anyRequest().permitAll().and().csrf().disable().cors().disable();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.jmsoftware.maf.authcenter.user.controller;

import com.jmsoftware.maf.authcenter.user.service.UserService;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.authcenter.user.LoginPayload;
import com.jmsoftware.maf.common.domain.authcenter.user.LoginResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
import javax.validation.Valid;

/**
* <h1>UserController</h1>
* <p>
* Controller implementation of UserPersistence.(UserPersistence)
*
* @author Johnny Miller (锺俊)
* @date 2020-05-10 12:08:28
*/
@Validated
@RestController
@RequiredArgsConstructor
public class UserController {
private final UserService userService;

@PostMapping("/users/login")
public ResponseBodyBean<LoginResponse> login(@Valid @RequestBody LoginPayload payload) {
return ResponseBodyBean.ofSuccess(userService.login(payload));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.baomidou.mybatisplus.extension.service.IService;
import com.jmsoftware.maf.authcenter.user.entity.UserPersistence;
import com.jmsoftware.maf.common.domain.authcenter.user.GetUserByLoginTokenResponse;
import com.jmsoftware.maf.common.domain.authcenter.user.SaveUserForRegisteringPayload;
import com.jmsoftware.maf.common.domain.authcenter.user.SaveUserForRegisteringResponse;
import com.jmsoftware.maf.common.domain.authcenter.user.*;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
Expand Down Expand Up @@ -35,4 +33,6 @@ public interface UserService extends IService<UserPersistence> {
* @return the save user for registering response
*/
SaveUserForRegisteringResponse saveUserForRegister(@Valid SaveUserForRegisteringPayload payload);

LoginResponse login(@Valid LoginPayload payload);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmsoftware.maf.authcenter.universal.service.JwtService;
import com.jmsoftware.maf.authcenter.user.entity.UserPersistence;
import com.jmsoftware.maf.authcenter.user.mapper.UserMapper;
import com.jmsoftware.maf.authcenter.user.service.UserService;
import com.jmsoftware.maf.common.domain.authcenter.user.GetUserByLoginTokenResponse;
import com.jmsoftware.maf.common.domain.authcenter.user.SaveUserForRegisteringPayload;
import com.jmsoftware.maf.common.domain.authcenter.user.SaveUserForRegisteringResponse;
import com.jmsoftware.maf.common.domain.authcenter.user.UserStatus;
import com.jmsoftware.maf.common.domain.authcenter.user.*;
import com.jmsoftware.maf.common.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
Expand All @@ -31,7 +32,11 @@
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPersistence> implements UserService {
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final JwtService jwtService;

@Override
public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginToken) {
LambdaQueryWrapper<UserPersistence> wrapper = Wrappers.lambdaQuery();
Expand Down Expand Up @@ -66,4 +71,19 @@ public SaveUserForRegisteringResponse saveUserForRegister(@Valid SaveUserForRegi
response.setUserId(userPersistence.getId());
return response;
}

@Override
@SneakyThrows
public LoginResponse login(@Valid LoginPayload payload) {
val user = this.getUserByLoginToken(payload.getLoginToken());
log.info("User: {}", user);
boolean matched = bCryptPasswordEncoder.matches(payload.getPassword(), user.getPassword());
if (matched) {
String jwt = jwtService.createJwt(payload.getRememberMe(), user.getId(), user.getUsername(), null, null);
val response = new LoginResponse();
response.setJwt(jwt);
return response;
}
throw new BusinessException("Login failure!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jmsoftware.maf.common.domain.authcenter.user;

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

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

/**
* Description: LoginPayload, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/22/2020 6:26 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,13 @@
package com.jmsoftware.maf.common.domain.authcenter.user;

import lombok.Data;

/**
* Description: LoginResponse, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/22/2020 6:27 PM
**/
@Data
public class LoginResponse {
private String jwt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jmsoftware.maf.common.domain.authcenter.user;

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

import javax.validation.constraints.NotEmpty;

/**
* Description: RegisterPayload, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/22/2020 6:27 PM
**/
@Data
public class SignupPayload {
/**
* Username (Unique)
*/
@NotEmpty
@Length(min = 4, max = 50)
private String username;
/**
* Email (Unique)
*/
@NotEmpty
@Length(max = 100)
private String email;
/**
* Password
*/
@NotEmpty
@Length(min = 8, max = 30)
private String password;
}

0 comments on commit fe3f211

Please sign in to comment.