feat: 登录授权接口
This commit is contained in:
@ -72,6 +72,9 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
package day.gitlab.dolphin.authorize;
|
||||
|
||||
import day.gitlab.dolphin.common.security.AuthenticationProvider;
|
||||
import day.gitlab.dolphin.common.security.UserPrincipal;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class DolphinAuthenticationProvider implements AuthenticationProvider {
|
||||
|
||||
@Override
|
||||
public UserPrincipal getUserPrincipal(String userId) {
|
||||
UserPrincipal userPrincipal = new UserPrincipal();
|
||||
userPrincipal.setId(userId);
|
||||
userPrincipal.setUsername(userId);
|
||||
userPrincipal.setNickname(userId);
|
||||
return userPrincipal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getUserAuthorities(String userId) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package day.gitlab.dolphin.authorize.controller;
|
||||
|
||||
import day.gitlab.dolphin.authorize.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.authorize.service.AuthorizeService;
|
||||
import day.gitlab.dolphin.common.core.entity.Result;
|
||||
import day.gitlab.dolphin.common.security.annotation.AuthorityIgnore;
|
||||
import jakarta.annotation.Resource;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/authorize")
|
||||
public class AuthorizeController {
|
||||
|
||||
@Resource
|
||||
private AuthorizeService authorizeService;
|
||||
|
||||
@AuthorityIgnore
|
||||
@PostMapping("/login")
|
||||
public Result login(@RequestBody LoginDTO loginDTO) {
|
||||
return Result.success(authorizeService.login(loginDTO));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package day.gitlab.dolphin.authorize.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginDTO {
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package day.gitlab.dolphin.authorize.entity.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginVO {
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private String refreshToken;
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package day.gitlab.dolphin.authorize.service;
|
||||
|
||||
import day.gitlab.dolphin.authorize.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.authorize.entity.vo.LoginVO;
|
||||
import day.gitlab.dolphin.common.security.AuthenticationProvider;
|
||||
|
||||
public interface AuthorizeService extends AuthenticationProvider {
|
||||
|
||||
LoginVO login(LoginDTO loginDTO);
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package day.gitlab.dolphin.authorize.service.impl;
|
||||
|
||||
import cn.hutool.v7.crypto.digest.BCrypt;
|
||||
import com.mybatisflex.core.query.QueryCondition;
|
||||
import day.gitlab.dolphin.authorize.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.authorize.entity.vo.LoginVO;
|
||||
import day.gitlab.dolphin.authorize.service.AuthorizeService;
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import day.gitlab.dolphin.common.security.UserPrincipal;
|
||||
import day.gitlab.dolphin.common.security.jwt.JwtInfo;
|
||||
import day.gitlab.dolphin.rbac.entity.Role;
|
||||
import day.gitlab.dolphin.rbac.entity.User;
|
||||
import day.gitlab.dolphin.rbac.entity.table.UserTableDef;
|
||||
import day.gitlab.dolphin.rbac.mapper.UserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class AuthorizeServiceImpl implements AuthorizeService {
|
||||
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Resource
|
||||
private JwtInfo jwtInfo;
|
||||
|
||||
@Override
|
||||
public UserPrincipal getUserPrincipal(String userId) {
|
||||
User user = userMapper.selectOneById(userId);
|
||||
|
||||
return UserPrincipal.builder()
|
||||
.id(user.getId())
|
||||
.username(user.getUsername())
|
||||
.nickname(user.getNickname())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getUserAuthorities(String userId) {
|
||||
User user = userMapper.selectOneWithRelationsById(userId);
|
||||
|
||||
List<Role> roles = user.getRoles();
|
||||
if (roles == null || roles.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return roles.stream().map(Role::getCode).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginVO login(LoginDTO loginDTO) {
|
||||
QueryCondition queryCondition = UserTableDef.USER.USERNAME.eq(loginDTO.getUsername());
|
||||
User user = userMapper.selectOneByCondition(queryCondition);
|
||||
|
||||
if (user == null || !BCrypt.checkpw(loginDTO.getPassword(), user.getPassword())) {
|
||||
throw new BusinessException("User name or password is incorrect");
|
||||
}
|
||||
if (!"1".equalsIgnoreCase(user.getEnabled())) {
|
||||
throw new BusinessException("User has been disabled");
|
||||
}
|
||||
|
||||
LoginVO loginVO = new LoginVO();
|
||||
loginVO.setAccessToken(jwtInfo.generateAccessToken(user.getId()));
|
||||
loginVO.setRefreshToken(jwtInfo.generateRefreshToken(user.getId()));
|
||||
return loginVO;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user