fix: checkstyle

This commit is contained in:
vran 2022-03-02 20:11:15 +08:00
parent 57fbdbbd22
commit dfeaac5292
9 changed files with 51 additions and 51 deletions

View File

@ -1,9 +1,9 @@
package com.databasir.api; package com.databasir.api;
import com.databasir.common.JsonData; import com.databasir.common.JsonData;
import com.databasir.core.domain.app.OAuthAppService; import com.databasir.core.domain.app.OpenAuthAppService;
import com.databasir.core.domain.app.data.*; import com.databasir.core.domain.app.data.*;
import com.databasir.core.domain.app.handler.OAuthHandler; import com.databasir.core.domain.app.handler.OpenAuthHandler;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -19,11 +19,11 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
@Controller @Controller
@RequiredArgsConstructor @RequiredArgsConstructor
public class OAuth2AppController { public class OpenAuth2AppController {
private final OAuthHandler oAuthHandler; private final OpenAuthHandler openAuthHandler;
private final OAuthAppService oAuthAppService; private final OpenAuthAppService openAuthAppService;
/** /**
* 无需授权 * 无需授权
@ -31,7 +31,7 @@ public class OAuth2AppController {
@GetMapping("/oauth2/authorization/{registrationId}") @GetMapping("/oauth2/authorization/{registrationId}")
@ResponseBody @ResponseBody
public JsonData<String> authorization(@PathVariable String registrationId) { public JsonData<String> authorization(@PathVariable String registrationId) {
String authorization = oAuthHandler.authorization(registrationId); String authorization = openAuthHandler.authorization(registrationId);
return JsonData.ok(authorization); return JsonData.ok(authorization);
} }
@ -41,7 +41,7 @@ public class OAuth2AppController {
@GetMapping("/oauth2/apps") @GetMapping("/oauth2/apps")
@ResponseBody @ResponseBody
public JsonData<List<OAuthAppResponse>> listApps() { public JsonData<List<OAuthAppResponse>> listApps() {
return JsonData.ok(oAuthAppService.listAll()); return JsonData.ok(openAuthAppService.listAll());
} }
@GetMapping(Routes.OAuth2App.LIST_PAGE) @GetMapping(Routes.OAuth2App.LIST_PAGE)
@ -50,14 +50,14 @@ public class OAuth2AppController {
public JsonData<Page<OAuthAppPageResponse>> listPage(@PageableDefault(sort = "id", direction = DESC) public JsonData<Page<OAuthAppPageResponse>> listPage(@PageableDefault(sort = "id", direction = DESC)
Pageable page, Pageable page,
OAuthAppPageCondition condition) { OAuthAppPageCondition condition) {
return JsonData.ok(oAuthAppService.listPage(page, condition)); return JsonData.ok(openAuthAppService.listPage(page, condition));
} }
@GetMapping(Routes.OAuth2App.GET_ONE) @GetMapping(Routes.OAuth2App.GET_ONE)
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") @PreAuthorize("hasAnyAuthority('SYS_OWNER')")
@ResponseBody @ResponseBody
public JsonData<OAuthAppDetailResponse> getOne(@PathVariable Integer id) { public JsonData<OAuthAppDetailResponse> getOne(@PathVariable Integer id) {
return JsonData.ok(oAuthAppService.getOne(id)); return JsonData.ok(openAuthAppService.getOne(id));
} }
@ -65,7 +65,7 @@ public class OAuth2AppController {
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") @PreAuthorize("hasAnyAuthority('SYS_OWNER')")
@ResponseBody @ResponseBody
public JsonData<Integer> create(@RequestBody @Valid OAuthAppCreateRequest request) { public JsonData<Integer> create(@RequestBody @Valid OAuthAppCreateRequest request) {
Integer id = oAuthAppService.create(request); Integer id = openAuthAppService.create(request);
return JsonData.ok(id); return JsonData.ok(id);
} }
@ -73,7 +73,7 @@ public class OAuth2AppController {
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") @PreAuthorize("hasAnyAuthority('SYS_OWNER')")
@ResponseBody @ResponseBody
public JsonData<Void> updateById(@RequestBody @Valid OAuthAppUpdateRequest request) { public JsonData<Void> updateById(@RequestBody @Valid OAuthAppUpdateRequest request) {
oAuthAppService.updateById(request); openAuthAppService.updateById(request);
return JsonData.ok(); return JsonData.ok();
} }
@ -81,7 +81,7 @@ public class OAuth2AppController {
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") @PreAuthorize("hasAnyAuthority('SYS_OWNER')")
@ResponseBody @ResponseBody
public JsonData<Void> deleteById(@PathVariable Integer id) { public JsonData<Void> deleteById(@PathVariable Integer id) {
oAuthAppService.deleteById(id); openAuthAppService.deleteById(id);
return JsonData.ok(); return JsonData.ok();
} }
} }

View File

@ -1,7 +1,6 @@
package com.databasir.api.config; package com.databasir.api.config;
import com.databasir.api.Routes; import com.databasir.api.Routes;
import com.databasir.api.config.oauth2.DatabasirOauth2LoginFilter;
import com.databasir.api.config.security.*; import com.databasir.api.config.security.*;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -46,7 +45,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.and() .and()
.authorizeRequests() .authorizeRequests()
.antMatchers("/login", Routes.Login.REFRESH_ACCESS_TOKEN).permitAll() .antMatchers("/login", Routes.Login.REFRESH_ACCESS_TOKEN).permitAll()
.antMatchers("/oauth2/apps", "/oauth2/failure", "/oauth2/authorization/*", "/oauth2/login/*").permitAll() .antMatchers("/oauth2/apps", "/oauth2/failure", "/oauth2/authorization/*", "/oauth2/login/*")
.permitAll()
.antMatchers("/", "/*.html", "/js/**", "/css/**", "/img/**", "/*.ico").permitAll() .antMatchers("/", "/*.html", "/js/**", "/css/**", "/img/**", "/*.ico").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()

View File

@ -2,7 +2,7 @@ package com.databasir.api.config.oauth2;
import com.databasir.api.config.security.DatabasirUserDetailService; import com.databasir.api.config.security.DatabasirUserDetailService;
import com.databasir.core.domain.user.data.UserDetailResponse; import com.databasir.core.domain.user.data.UserDetailResponse;
import com.databasir.core.domain.app.OAuthAppService; import com.databasir.core.domain.app.OpenAuthAppService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
@ -28,16 +28,16 @@ public class DatabasirOauth2LoginFilter extends AbstractAuthenticationProcessing
public static final String OAUTH_LOGIN_URI = "/oauth2/login/*"; public static final String OAUTH_LOGIN_URI = "/oauth2/login/*";
@Autowired @Autowired
private OAuthAppService oAuthAppService; private OpenAuthAppService openAuthAppService;
@Autowired @Autowired
private DatabasirUserDetailService databasirUserDetailService; private DatabasirUserDetailService databasirUserDetailService;
public DatabasirOauth2LoginFilter(AuthenticationManager authenticationManager, public DatabasirOauth2LoginFilter(AuthenticationManager authenticationManager,
OAuth2AuthenticationSuccessHandler oAuth2AuthenticationSuccessHandler, OAuth2AuthenticationSuccessHandler auth2AuthenticationSuccessHandler,
AuthenticationFailureHandler authenticationFailureHandler) { AuthenticationFailureHandler authenticationFailureHandler) {
super(OAUTH_LOGIN_URI, authenticationManager); super(OAUTH_LOGIN_URI, authenticationManager);
this.setAuthenticationSuccessHandler(oAuth2AuthenticationSuccessHandler); this.setAuthenticationSuccessHandler(auth2AuthenticationSuccessHandler);
this.setAuthenticationFailureHandler(authenticationFailureHandler); this.setAuthenticationFailureHandler(authenticationFailureHandler);
} }
@ -46,7 +46,7 @@ public class DatabasirOauth2LoginFilter extends AbstractAuthenticationProcessing
throws AuthenticationException, IOException, ServletException { throws AuthenticationException, IOException, ServletException {
Map<String, String[]> params = request.getParameterMap(); Map<String, String[]> params = request.getParameterMap();
String registrationId = new AntPathMatcher().extractPathWithinPattern(OAUTH_LOGIN_URI, request.getRequestURI()); String registrationId = new AntPathMatcher().extractPathWithinPattern(OAUTH_LOGIN_URI, request.getRequestURI());
UserDetailResponse userDetailResponse = oAuthAppService.oauthCallback(registrationId, params); UserDetailResponse userDetailResponse = openAuthAppService.oauthCallback(registrationId, params);
UserDetails details = databasirUserDetailService.loadUserByUsername(userDetailResponse.getUsername()); UserDetails details = databasirUserDetailService.loadUserByUsername(userDetailResponse.getUsername());
DatabasirOAuth2Authentication authentication = new DatabasirOAuth2Authentication(details); DatabasirOAuth2Authentication authentication = new DatabasirOAuth2Authentication(details);
if (!userDetailResponse.getEnabled()) { if (!userDetailResponse.getEnabled()) {

View File

@ -38,7 +38,6 @@ public class JsonData<T> {
return jsonData; return jsonData;
} }
public static <T> JsonData<T> error(String errorCode, String errMessage) { public static <T> JsonData<T> error(String errorCode, String errMessage) {
JsonData<T> jsonData = new JsonData<>(); JsonData<T> jsonData = new JsonData<>();
jsonData.setErrCode(errorCode); jsonData.setErrCode(errorCode);

View File

@ -4,13 +4,13 @@ import com.databasir.core.domain.DomainErrors;
import com.databasir.core.domain.app.converter.OAuthAppPojoConverter; import com.databasir.core.domain.app.converter.OAuthAppPojoConverter;
import com.databasir.core.domain.app.converter.OAuthAppResponseConverter; import com.databasir.core.domain.app.converter.OAuthAppResponseConverter;
import com.databasir.core.domain.app.data.*; import com.databasir.core.domain.app.data.*;
import com.databasir.core.domain.app.handler.OAuthHandler; import com.databasir.core.domain.app.handler.OpenAuthHandler;
import com.databasir.core.domain.app.handler.OAuthProcessContext; import com.databasir.core.domain.app.handler.OAuthProcessContext;
import com.databasir.core.domain.app.handler.OAuthProcessResult; import com.databasir.core.domain.app.handler.OAuthProcessResult;
import com.databasir.core.domain.user.data.UserCreateRequest; import com.databasir.core.domain.user.data.UserCreateRequest;
import com.databasir.core.domain.user.data.UserDetailResponse; import com.databasir.core.domain.user.data.UserDetailResponse;
import com.databasir.core.domain.user.service.UserService; import com.databasir.core.domain.user.service.UserService;
import com.databasir.dao.impl.OAuthAppDao; import com.databasir.dao.impl.OauthAppDao;
import com.databasir.dao.tables.pojos.OauthAppPojo; import com.databasir.dao.tables.pojos.OauthAppPojo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
@ -27,23 +27,23 @@ import java.util.stream.Collectors;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class OAuthAppService { public class OpenAuthAppService {
private final List<OAuthHandler> oAuthHandlers; private final List<OpenAuthHandler> openAuthHandlers;
private final OAuthAppDao oAuthAppDao; private final OauthAppDao oauthAppDao;
private final UserService userService; private final UserService userService;
private final OAuthAppResponseConverter oAuthAppResponseConverter; private final OAuthAppResponseConverter oauthAppResponseConverter;
private final OAuthAppPojoConverter oAuthAppPojoConverter; private final OAuthAppPojoConverter oauthAppPojoConverter;
public UserDetailResponse oauthCallback(String registrationId, Map<String, String[]> params) { public UserDetailResponse oauthCallback(String registrationId, Map<String, String[]> params) {
// match handler // match handler
OauthAppPojo app = oAuthAppDao.selectByRegistrationId(registrationId); OauthAppPojo app = oauthAppDao.selectByRegistrationId(registrationId);
OAuthHandler oAuthHandler = oAuthHandlers.stream() OpenAuthHandler openAuthHandler = openAuthHandlers.stream()
.filter(handler -> handler.support(app.getAppType())) .filter(handler -> handler.support(app.getAppType()))
.findFirst() .findFirst()
.orElseThrow(() -> new UsernameNotFoundException("暂不支持该类型登陆")); .orElseThrow(() -> new UsernameNotFoundException("暂不支持该类型登陆"));
@ -53,7 +53,7 @@ public class OAuthAppService {
.callbackParameters(params) .callbackParameters(params)
.registrationId(registrationId) .registrationId(registrationId)
.build(); .build();
OAuthProcessResult result = oAuthHandler.process(context); OAuthProcessResult result = openAuthHandler.process(context);
// get or create new user // get or create new user
return userService.get(result.getEmail()) return userService.get(result.getEmail())
@ -71,41 +71,41 @@ public class OAuthAppService {
} }
public List<OAuthAppResponse> listAll() { public List<OAuthAppResponse> listAll() {
List<OauthAppPojo> apps = oAuthAppDao.selectAll(); List<OauthAppPojo> apps = oauthAppDao.selectAll();
return apps.stream() return apps.stream()
.map(oAuthAppResponseConverter::to) .map(oauthAppResponseConverter::to)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public void deleteById(Integer id) { public void deleteById(Integer id) {
if (oAuthAppDao.existsById(id)) { if (oauthAppDao.existsById(id)) {
oAuthAppDao.deleteById(id); oauthAppDao.deleteById(id);
} }
} }
public void updateById(OAuthAppUpdateRequest request) { public void updateById(OAuthAppUpdateRequest request) {
OauthAppPojo pojo = oAuthAppPojoConverter.of(request); OauthAppPojo pojo = oauthAppPojoConverter.of(request);
try { try {
oAuthAppDao.updateById(pojo); oauthAppDao.updateById(pojo);
} catch (DuplicateKeyException e) { } catch (DuplicateKeyException e) {
throw DomainErrors.REGISTRATION_ID_DUPLICATE.exception(); throw DomainErrors.REGISTRATION_ID_DUPLICATE.exception();
} }
} }
public Integer create(OAuthAppCreateRequest request) { public Integer create(OAuthAppCreateRequest request) {
OauthAppPojo pojo = oAuthAppPojoConverter.of(request); OauthAppPojo pojo = oauthAppPojoConverter.of(request);
try { try {
return oAuthAppDao.insertAndReturnId(pojo); return oauthAppDao.insertAndReturnId(pojo);
} catch (DuplicateKeyException e) { } catch (DuplicateKeyException e) {
throw DomainErrors.REGISTRATION_ID_DUPLICATE.exception(); throw DomainErrors.REGISTRATION_ID_DUPLICATE.exception();
} }
} }
public Page<OAuthAppPageResponse> listPage(Pageable page, OAuthAppPageCondition condition) { public Page<OAuthAppPageResponse> listPage(Pageable page, OAuthAppPageCondition condition) {
return oAuthAppDao.selectByPage(page, condition.toCondition()).map(oAuthAppPojoConverter::toPageResponse); return oauthAppDao.selectByPage(page, condition.toCondition()).map(oauthAppPojoConverter::toPageResponse);
} }
public Optional<OAuthAppDetailResponse> getOne(Integer id) { public Optional<OAuthAppDetailResponse> getOne(Integer id) {
return oAuthAppDao.selectOptionalById(id).map(oAuthAppPojoConverter::toDetailResponse); return oauthAppDao.selectOptionalById(id).map(oauthAppPojoConverter::toDetailResponse);
} }
} }

View File

@ -1,11 +1,10 @@
package com.databasir.core.domain.app.handler; package com.databasir.core.domain.app.handler;
import com.databasir.core.domain.DomainErrors; import com.databasir.core.domain.DomainErrors;
import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; import com.databasir.core.domain.app.exception.DatabasirAuthenticationException;
import com.databasir.core.infrastructure.remote.github.GithubRemoteService; import com.databasir.core.infrastructure.remote.github.GithubRemoteService;
import com.databasir.dao.enums.OAuthAppType; import com.databasir.dao.enums.OAuthAppType;
import com.databasir.dao.impl.OAuthAppDao; import com.databasir.dao.impl.OauthAppDao;
import com.databasir.dao.tables.pojos.OauthAppPojo; import com.databasir.dao.tables.pojos.OauthAppPojo;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -18,11 +17,11 @@ import java.util.Map;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class GithubOauthHandler implements OAuthHandler { public class GithubOpenAuthHandler implements OpenAuthHandler {
private final GithubRemoteService githubRemoteService; private final GithubRemoteService githubRemoteService;
private final OAuthAppDao oAuthAppDao; private final OauthAppDao oauthAppDao;
@Override @Override
public boolean support(String oauthAppType) { public boolean support(String oauthAppType) {
@ -31,7 +30,7 @@ public class GithubOauthHandler implements OAuthHandler {
@Override @Override
public String authorization(String registrationId) { public String authorization(String registrationId) {
OauthAppPojo app = oAuthAppDao.selectByRegistrationId(registrationId); OauthAppPojo app = oauthAppDao.selectByRegistrationId(registrationId);
String authUrl = app.getAuthUrl(); String authUrl = app.getAuthUrl();
String clientId = app.getClientId(); String clientId = app.getClientId();
String authorizeUrl = authUrl + "/login/oauth/authorize"; String authorizeUrl = authUrl + "/login/oauth/authorize";
@ -46,7 +45,7 @@ public class GithubOauthHandler implements OAuthHandler {
@Override @Override
public OAuthProcessResult process(OAuthProcessContext context) { public OAuthProcessResult process(OAuthProcessContext context) {
OauthAppPojo authApp = oAuthAppDao.selectByRegistrationId(context.getRegistrationId()); OauthAppPojo authApp = oauthAppDao.selectByRegistrationId(context.getRegistrationId());
String clientId = authApp.getClientId(); String clientId = authApp.getClientId();
String clientSecret = authApp.getClientSecret(); String clientSecret = authApp.getClientSecret();
String baseUrl = authApp.getResourceUrl(); String baseUrl = authApp.getResourceUrl();

View File

@ -1,6 +1,6 @@
package com.databasir.core.domain.app.handler; package com.databasir.core.domain.app.handler;
public interface OAuthHandler { public interface OpenAuthHandler {
boolean support(String oauthAppType); boolean support(String oauthAppType);

View File

@ -111,10 +111,12 @@ public class LoginService {
data.setUsername(user.getUsername()); data.setUsername(user.getUsername());
data.setAccessToken(login.getAccessToken()); data.setAccessToken(login.getAccessToken());
data.setAvatar(user.getAvatar()); data.setAvatar(user.getAvatar());
long expireAt = login.getAccessTokenExpireAt().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); long expireAt = login.getAccessTokenExpireAt()
.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
data.setAccessTokenExpireAt(expireAt); data.setAccessTokenExpireAt(expireAt);
data.setRefreshToken(login.getRefreshToken()); data.setRefreshToken(login.getRefreshToken());
List<UserRolePojo> rolePojoList = userRoleDao.selectByUserIds(Collections.singletonList(user.getId())); List<UserRolePojo> rolePojoList =
userRoleDao.selectByUserIds(Collections.singletonList(user.getId()));
List<UserLoginResponse.RoleResponse> roles = rolePojoList List<UserLoginResponse.RoleResponse> roles = rolePojoList
.stream() .stream()
.map(ur -> { .map(ur -> {

View File

@ -12,13 +12,13 @@ import java.util.Optional;
import static com.databasir.dao.Tables.OAUTH_APP; import static com.databasir.dao.Tables.OAUTH_APP;
@Repository @Repository
public class OAuthAppDao extends BaseDao<OauthAppPojo> { public class OauthAppDao extends BaseDao<OauthAppPojo> {
@Autowired @Autowired
@Getter @Getter
private DSLContext dslContext; private DSLContext dslContext;
public OAuthAppDao() { public OauthAppDao() {
super(OAUTH_APP, OauthAppPojo.class); super(OAUTH_APP, OauthAppPojo.class);
} }