mirror of https://github.com/longtai-cn/hippo4j
parent
fe979378c2
commit
266e72caf0
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.auth.mapper;
|
||||
|
||||
import cn.hippo4j.auth.model.ConsumerInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/10/29 13:02
|
||||
*@description:
|
||||
*/
|
||||
@Mapper
|
||||
public interface ConsumerMapper extends BaseMapper<ConsumerInfo> {
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.auth.mapper;
|
||||
|
||||
import cn.hippo4j.auth.model.ConsumerTokenInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/10/29 13:02
|
||||
*@description:
|
||||
*/
|
||||
@Mapper
|
||||
public interface ConsumerTokenMapper extends BaseMapper<ConsumerTokenInfo> {
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.auth.model.biz.conmuser;
|
||||
|
||||
import com.sun.istack.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/10/29 13:12
|
||||
*@description:
|
||||
*/
|
||||
@Data
|
||||
public class ConsumerDTO {
|
||||
|
||||
/**
|
||||
* appid
|
||||
*/
|
||||
@NotNull
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 应用名
|
||||
*/
|
||||
@NotNull
|
||||
private String name;
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.auth.service;
|
||||
|
||||
import cn.hippo4j.auth.model.ConsumerInfo;
|
||||
import cn.hippo4j.auth.model.ConsumerTokenInfo;
|
||||
import cn.hippo4j.auth.model.biz.conmuser.ConsumerDTO;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/10/29 13:10
|
||||
*@description:
|
||||
*/
|
||||
public interface ConsumerService {
|
||||
|
||||
ConsumerInfo createConsumer(ConsumerDTO consumerDTO);
|
||||
|
||||
ConsumerTokenInfo generateAndSaveConsumerToken(ConsumerInfo consumer);
|
||||
|
||||
Integer getConsumerId(String tokenHeader);
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.auth.service.impl;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import cn.hippo4j.auth.mapper.ConsumerMapper;
|
||||
import cn.hippo4j.auth.mapper.ConsumerTokenMapper;
|
||||
import cn.hippo4j.auth.model.ConsumerDO;
|
||||
import cn.hippo4j.auth.model.ConsumerInfo;
|
||||
import cn.hippo4j.auth.model.ConsumerTokenInfo;
|
||||
import cn.hippo4j.auth.model.biz.conmuser.ConsumerDTO;
|
||||
import cn.hippo4j.auth.security.JwtTokenManager;
|
||||
import cn.hippo4j.auth.service.ConsumerService;
|
||||
import cn.hippo4j.common.toolkit.StringUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/10/29 13:10
|
||||
*@description:
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ConsumerServiceImpl implements ConsumerService {
|
||||
|
||||
private static final Date DEFAULT_EXPIRES = new GregorianCalendar(2099, Calendar.JANUARY, 1).getTime();
|
||||
|
||||
private final ConsumerMapper consumerMapper;
|
||||
|
||||
private final ConsumerTokenMapper consumerTokenMapper;
|
||||
|
||||
private final JwtTokenManager jwtTokenManager;
|
||||
|
||||
@Override
|
||||
public ConsumerInfo createConsumer(ConsumerDTO consumerDTO) {
|
||||
ConsumerInfo consumerDO = new ConsumerInfo();
|
||||
consumerDO.setAppId(consumerDTO.getAppId());
|
||||
consumerDO.setName(consumerDTO.getName());
|
||||
consumerMapper.insert(consumerDO);
|
||||
return consumerDO;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConsumerTokenInfo generateAndSaveConsumerToken(ConsumerInfo consumer) {
|
||||
String token = jwtTokenManager.createToken(consumer.getName(), DEFAULT_EXPIRES);
|
||||
ConsumerTokenInfo consumerTokenDO = new ConsumerTokenInfo();
|
||||
consumerTokenDO.setConsumerId(consumer.getId());
|
||||
consumerTokenDO.setToken(token);
|
||||
consumerTokenMapper.insert(consumerTokenDO);
|
||||
return consumerTokenDO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getConsumerId(String tokenHeader) {
|
||||
if (StringUtil.isEmpty(tokenHeader)) {
|
||||
return null;
|
||||
}
|
||||
LambdaUpdateWrapper<ConsumerTokenInfo> wrapper = Wrappers.lambdaUpdate();
|
||||
wrapper.eq(ConsumerTokenInfo::getToken, tokenHeader);
|
||||
ConsumerTokenInfo consumerTokenDO = consumerTokenMapper.selectOne(wrapper);
|
||||
|
||||
return ObjectUtils.isEmpty(consumerTokenDO) ? null : consumerTokenDO.getConsumerId();
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.console.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import cn.hippo4j.auth.model.ConsumerInfo;
|
||||
import cn.hippo4j.auth.model.ConsumerTokenInfo;
|
||||
import cn.hippo4j.auth.model.biz.conmuser.ConsumerDTO;
|
||||
import cn.hippo4j.auth.service.ConsumerService;
|
||||
import cn.hippo4j.common.web.base.Result;
|
||||
import cn.hippo4j.common.web.base.Results;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
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;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/10/29 13:04
|
||||
*@description:
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/consumer")
|
||||
public class ConsumerController {
|
||||
|
||||
private final ConsumerService consumerService;
|
||||
|
||||
/**
|
||||
* create consumer and token
|
||||
* @param consumerDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping()
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Result<ConsumerTokenInfo> createConsumer(@Valid @RequestBody ConsumerDTO consumerDTO) {
|
||||
ConsumerInfo consumer = consumerService.createConsumer(consumerDTO);
|
||||
ConsumerTokenInfo consumerTokenDO = consumerService.generateAndSaveConsumerToken(consumer);
|
||||
return Results.success(consumerTokenDO);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue