基础框架 75%
This commit is contained in:
parent
de54e6789a
commit
4bf73e585c
|
@ -4,7 +4,9 @@ import lombok.ToString;
|
|||
|
||||
@ToString
|
||||
public enum CodeMsg {
|
||||
SUCCESS(200, "成功");
|
||||
SUCCESS(200, "成功"),
|
||||
FAILURE(500, "失败"),
|
||||
AUTH_FAILURE(403, "认证失败");
|
||||
|
||||
private Integer code;
|
||||
private String message;
|
||||
|
@ -17,6 +19,7 @@ public enum CodeMsg {
|
|||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package com.common.info;
|
||||
|
||||
public class Parameter {
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
package com.zbsz.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
@Configuration
|
||||
public class JsonConfig {
|
||||
@Bean
|
||||
Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){
|
||||
return new Jackson2ObjectMapperBuilderCustomizer() {
|
||||
@Override
|
||||
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
|
||||
jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.zbsz.config;
|
||||
|
||||
import com.zbsz.filter.JwtAuthenticationInterceptor;
|
||||
import com.zbsz.filter.JwtInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
@ -27,12 +27,12 @@ public class Swagger2Config implements WebMvcConfigurer {
|
|||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(new ApiInfoBuilder()
|
||||
.title("Event Bridge API")
|
||||
.description("Event Bridge API 说明")
|
||||
.title("指标设置 API")
|
||||
.description("指标设置 API 说明")
|
||||
.version("V-1.0")
|
||||
.build())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.rest.controller"))
|
||||
.apis(RequestHandlerSelectors.basePackage("com.zbsz.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
@ -41,14 +41,14 @@ public class Swagger2Config implements WebMvcConfigurer {
|
|||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//添加拦截器
|
||||
registry.addInterceptor(new JwtAuthenticationInterceptor()).addPathPatterns("/api/*")
|
||||
registry.addInterceptor(new JwtInterceptor()).addPathPatterns("/api/*")
|
||||
.excludePathPatterns(
|
||||
"/api/login");//放掉某些特定不需要校验token的路由
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JwtAuthenticationInterceptor authenticationInterceptor() {
|
||||
return new JwtAuthenticationInterceptor();
|
||||
public JwtInterceptor authenticationInterceptor() {
|
||||
return new JwtInterceptor();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package com.zbsz.controller;
|
||||
|
||||
import com.common.info.CodeMsg;
|
||||
import com.zbsz.entity.User;
|
||||
import com.zbsz.info.LoginInfo;
|
||||
import com.zbsz.info.Result;
|
||||
import com.zbsz.service.UserService;
|
||||
import com.zbsz.tool.JwtTool;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
@Api(tags = "服务接口")
|
||||
public class AuthController {
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 登录表单需要校验字段是否含有 or 1=1
|
||||
* @param info
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "登录",notes = "登录服务")
|
||||
@PostMapping("login")
|
||||
public Result login(@Valid LoginInfo info) {
|
||||
Result result = new Result();
|
||||
try {
|
||||
User user = userService.lambdaQuery().eq(User::getName,info.getUserName()).eq(User::getPassWord,info.getPassWord()).one();
|
||||
if(user!=null){
|
||||
result.setCode(CodeMsg.SUCCESS.getCode());
|
||||
result.setMsg(CodeMsg.SUCCESS.getMessage());
|
||||
}else {
|
||||
result.setCode(CodeMsg.AUTH_FAILURE.getCode());
|
||||
result.setMsg(CodeMsg.AUTH_FAILURE.getMessage());
|
||||
result.setData(JwtTool.getToken(info.getUserName()));
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("-------- AuthController login 异常 : "+e.getMessage(),e);
|
||||
result.setCode(CodeMsg.FAILURE.getCode());
|
||||
result.setMsg(e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,13 +1,18 @@
|
|||
package com.zbsz.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName(value = "user")
|
||||
public class User {
|
||||
@TableId
|
||||
private Long id;
|
||||
private String name;
|
||||
@TableField("pass_word")
|
||||
private String passWord;
|
||||
private Integer age;
|
||||
private String email;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.zbsz.filter;
|
|||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.zbsz.info.Result;
|
||||
import com.zbsz.tool.JwtTool;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
|
@ -10,7 +11,8 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class JwtAuthenticationInterceptor implements HandlerInterceptor {
|
||||
@Slf4j
|
||||
public class JwtInterceptor implements HandlerInterceptor {
|
||||
|
||||
//判断然后进行用户拦截
|
||||
@Override
|
|
@ -0,0 +1,13 @@
|
|||
package com.zbsz.info;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class LoginInfo {
|
||||
@NotBlank(message = "用户名称不能为空")
|
||||
private String userName; //操作人名称
|
||||
@NotBlank(message = "用户密码不能为空")
|
||||
private String passWord; //操作人密码
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.zbsz.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zbsz.entity.User;
|
||||
import com.zbsz.mapper.UserMapper;
|
||||
|
||||
public class UserService extends ServiceImpl<UserMapper, User> {
|
||||
}
|
|
@ -12,7 +12,9 @@ spring:
|
|||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ant_path_matcher
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
c3p0:
|
||||
# jdbcUrl: jdbc:mysql://82.157.153.250:3306/hp_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
|
||||
|
|
Loading…
Reference in New Issue