00019 Register a new client_details
parent
57b7f42386
commit
dd2d613f45
|
@ -2,6 +2,7 @@ package cc.wdcy.domain.dto;
|
||||||
|
|
||||||
import cc.wdcy.domain.oauth.OauthClientDetails;
|
import cc.wdcy.domain.oauth.OauthClientDetails;
|
||||||
import cc.wdcy.infrastructure.DateUtils;
|
import cc.wdcy.infrastructure.DateUtils;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -196,4 +197,30 @@ public class OauthClientDetailsDto implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public OauthClientDetails createDomain() {
|
||||||
|
OauthClientDetails clientDetails = new OauthClientDetails()
|
||||||
|
.clientId(clientId)
|
||||||
|
.clientSecret(clientSecret)
|
||||||
|
.resourceIds(resourceIds)
|
||||||
|
.authorizedGrantTypes(authorizedGrantTypes)
|
||||||
|
.scope(scope);
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(webServerRedirectUri)) {
|
||||||
|
clientDetails.webServerRedirectUri(webServerRedirectUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(authorities)) {
|
||||||
|
clientDetails.authorities(authorities);
|
||||||
|
}
|
||||||
|
|
||||||
|
clientDetails.accessTokenValidity(accessTokenValidity)
|
||||||
|
.refreshTokenValidity(refreshTokenValidity)
|
||||||
|
.trusted(trusted);
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(additionalInformation)) {
|
||||||
|
clientDetails.additionalInformation(additionalInformation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientDetails;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -141,4 +141,59 @@ public class OauthClientDetails implements Serializable {
|
||||||
sb.append('}');
|
sb.append('}');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails clientId(String clientId) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails clientSecret(String clientSecret) {
|
||||||
|
this.clientSecret = clientSecret;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails resourceIds(String resourceIds) {
|
||||||
|
this.resourceIds = resourceIds;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails authorizedGrantTypes(String authorizedGrantTypes) {
|
||||||
|
this.authorizedGrantTypes = authorizedGrantTypes;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails scope(String scope) {
|
||||||
|
this.scope = scope;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails webServerRedirectUri(String webServerRedirectUri) {
|
||||||
|
this.webServerRedirectUri = webServerRedirectUri;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails authorities(String authorities) {
|
||||||
|
this.authorities = authorities;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails accessTokenValidity(Integer accessTokenValidity) {
|
||||||
|
this.accessTokenValidity = accessTokenValidity;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails refreshTokenValidity(Integer refreshTokenValidity) {
|
||||||
|
this.refreshTokenValidity = refreshTokenValidity;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails trusted(boolean trusted) {
|
||||||
|
this.trusted = trusted;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OauthClientDetails additionalInformation(String additionalInformation) {
|
||||||
|
this.additionalInformation = additionalInformation;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -15,4 +15,6 @@ public interface OauthRepository extends Repository {
|
||||||
List<OauthClientDetails> findAllOauthClientDetails();
|
List<OauthClientDetails> findAllOauthClientDetails();
|
||||||
|
|
||||||
void updateOauthClientDetailsArchive(@Param("clientId") String clientId, @Param("archive") boolean archive);
|
void updateOauthClientDetailsArchive(@Param("clientId") String clientId, @Param("archive") boolean archive);
|
||||||
|
|
||||||
|
void saveOauthClientDetails(OauthClientDetails clientDetails);
|
||||||
}
|
}
|
|
@ -18,4 +18,6 @@ public interface OauthService {
|
||||||
void archiveOauthClientDetails(String clientId);
|
void archiveOauthClientDetails(String clientId);
|
||||||
|
|
||||||
OauthClientDetailsDto loadOauthClientDetailsDto(String clientId);
|
OauthClientDetailsDto loadOauthClientDetailsDto(String clientId);
|
||||||
|
|
||||||
|
void registerClientDetails(OauthClientDetailsDto formDto);
|
||||||
}
|
}
|
|
@ -37,6 +37,12 @@ public class OauthServiceImpl implements OauthService {
|
||||||
@Override
|
@Override
|
||||||
public OauthClientDetailsDto loadOauthClientDetailsDto(String clientId) {
|
public OauthClientDetailsDto loadOauthClientDetailsDto(String clientId) {
|
||||||
final OauthClientDetails oauthClientDetails = oauthRepository.findOauthClientDetails(clientId);
|
final OauthClientDetails oauthClientDetails = oauthRepository.findOauthClientDetails(clientId);
|
||||||
return new OauthClientDetailsDto(oauthClientDetails);
|
return oauthClientDetails != null ? new OauthClientDetailsDto(oauthClientDetails) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerClientDetails(OauthClientDetailsDto formDto) {
|
||||||
|
OauthClientDetails clientDetails = formDto.createDomain();
|
||||||
|
oauthRepository.saveOauthClientDetails(clientDetails);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,9 +2,12 @@ package cc.wdcy.web.controller;
|
||||||
|
|
||||||
import cc.wdcy.domain.dto.OauthClientDetailsDto;
|
import cc.wdcy.domain.dto.OauthClientDetailsDto;
|
||||||
import cc.wdcy.service.OauthService;
|
import cc.wdcy.service.OauthService;
|
||||||
|
import cc.wdcy.web.oauth.OauthClientDetailsDtoValidator;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
@ -23,6 +26,9 @@ public class ClientDetailsController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private OauthService oauthService;
|
private OauthService oauthService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OauthClientDetailsDtoValidator clientDetailsDtoValidator;
|
||||||
|
|
||||||
|
|
||||||
@RequestMapping("client_details")
|
@RequestMapping("client_details")
|
||||||
public String clientDetails(Model model) {
|
public String clientDetails(Model model) {
|
||||||
|
@ -62,4 +68,18 @@ public class ClientDetailsController {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Submit register client
|
||||||
|
* */
|
||||||
|
@RequestMapping(value = "register_client", method = RequestMethod.POST)
|
||||||
|
public String submitRegisterClient(@ModelAttribute("formDto") OauthClientDetailsDto formDto, BindingResult result) {
|
||||||
|
clientDetailsDtoValidator.validate(formDto, result);
|
||||||
|
if (result.hasErrors()) {
|
||||||
|
return "clientdetails/register_client";
|
||||||
|
}
|
||||||
|
oauthService.registerClientDetails(formDto);
|
||||||
|
return "redirect:client_details";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
package cc.wdcy.web.oauth;
|
||||||
|
|
||||||
|
import cc.wdcy.domain.dto.OauthClientDetailsDto;
|
||||||
|
import cc.wdcy.service.OauthService;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.validation.Errors;
|
||||||
|
import org.springframework.validation.Validator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Shengzhao Li
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class OauthClientDetailsDtoValidator implements Validator {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OauthService oauthService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(Class<?> clazz) {
|
||||||
|
return OauthClientDetailsDto.class.equals(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validate(Object target, Errors errors) {
|
||||||
|
OauthClientDetailsDto clientDetailsDto = (OauthClientDetailsDto) target;
|
||||||
|
|
||||||
|
validateClientId(clientDetailsDto, errors);
|
||||||
|
validateClientSecret(clientDetailsDto, errors);
|
||||||
|
|
||||||
|
validateGrantTypes(clientDetailsDto, errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateGrantTypes(OauthClientDetailsDto clientDetailsDto, Errors errors) {
|
||||||
|
final String grantTypes = clientDetailsDto.getAuthorizedGrantTypes();
|
||||||
|
if (StringUtils.isEmpty(grantTypes)) {
|
||||||
|
errors.rejectValue("authorizedGrantTypes", null, "grant_type(s) is required");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateClientSecret(OauthClientDetailsDto clientDetailsDto, Errors errors) {
|
||||||
|
final String clientSecret = clientDetailsDto.getClientSecret();
|
||||||
|
if (StringUtils.isEmpty(clientSecret)) {
|
||||||
|
errors.rejectValue("clientSecret", null, "client_secret is required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clientSecret.length() < 8) {
|
||||||
|
errors.rejectValue("clientSecret", null, "client_secret 长度至少8位");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateClientId(OauthClientDetailsDto clientDetailsDto, Errors errors) {
|
||||||
|
final String clientId = clientDetailsDto.getClientId();
|
||||||
|
if (StringUtils.isEmpty(clientId)) {
|
||||||
|
errors.rejectValue("clientId", null, "client_id is required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clientId.length() < 5) {
|
||||||
|
errors.rejectValue("clientId", null, "client_id 长度至少5位");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
OauthClientDetailsDto clientDetailsDto1 = oauthService.loadOauthClientDetailsDto(clientId);
|
||||||
|
if (clientDetailsDto1 != null) {
|
||||||
|
errors.rejectValue("clientId", null, "client_id [" + clientId + "] 已存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,4 +39,13 @@
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
|
||||||
|
<insert id="saveOauthClientDetails" parameterType="OauthClientDetails">
|
||||||
|
insert into oauth_client_details(client_id,resource_ids,client_secret,scope,authorized_grant_types,web_server_redirect_uri,
|
||||||
|
authorities,access_token_validity,refresh_token_validity,additional_information,trusted)
|
||||||
|
values
|
||||||
|
(#{clientId},#{resourceIds},#{clientSecret},#{scope},#{authorizedGrantTypes}, #{webServerRedirectUri},
|
||||||
|
#{authorities},#{accessTokenValidity},#{refreshTokenValidity},#{additionalInformation}, #{trusted})
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
|
@ -195,6 +195,15 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="col-sm-2"></div>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<form:errors cssClass="text-danger"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-sm-2"></div>
|
<div class="col-sm-2"></div>
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cc.wdcy.infrastructure.mybatis;
|
||||||
|
|
||||||
import cc.wdcy.domain.oauth.OauthClientDetails;
|
import cc.wdcy.domain.oauth.OauthClientDetails;
|
||||||
import cc.wdcy.domain.oauth.OauthRepository;
|
import cc.wdcy.domain.oauth.OauthRepository;
|
||||||
|
import cc.wdcy.domain.shared.GuidGenerator;
|
||||||
import cc.wdcy.infrastructure.AbstractRepositoryTest;
|
import cc.wdcy.infrastructure.AbstractRepositoryTest;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
@ -27,6 +28,22 @@ public class OauthRepositoryMyBatisTest extends AbstractRepositoryTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void saveOauthClientDetails() {
|
||||||
|
|
||||||
|
final String clientId = GuidGenerator.generate();
|
||||||
|
|
||||||
|
OauthClientDetails clientDetails = new OauthClientDetails().clientId(clientId);
|
||||||
|
oauthRepositoryMyBatis.saveOauthClientDetails(clientDetails);
|
||||||
|
|
||||||
|
final OauthClientDetails oauthClientDetails = oauthRepositoryMyBatis.findOauthClientDetails(clientId);
|
||||||
|
assertNotNull(oauthClientDetails);
|
||||||
|
assertNotNull(oauthClientDetails.clientId());
|
||||||
|
assertNull(oauthClientDetails.clientSecret());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findAllOauthClientDetails() {
|
public void findAllOauthClientDetails() {
|
||||||
final List<OauthClientDetails> allOauthClientDetails = oauthRepositoryMyBatis.findAllOauthClientDetails();
|
final List<OauthClientDetails> allOauthClientDetails = oauthRepositoryMyBatis.findAllOauthClientDetails();
|
||||||
|
|
Loading…
Reference in New Issue