io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
代码如下
@Configuration
public class SwaggerConfig {
//是否开启swagger,根据环境来选择
@Value(value = "${swagger.enabled}")
Boolean swaggerEnabled;
@Bean
public Docket Lamp() {
Parameter token = new ParameterBuilder().name("token") //全局参数
.description("用户登陆令牌")
.parameterType("header")
.modelRef(new ModelRef("String"))
.required(true)
.build();
ArrayList parameters = new ArrayList<>();
parameters.add(token);
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(parameters)
.apiInfo(apiInfo())
.groupName("灯")
.select()
.apis(RequestHandlerSelectors.basePackage("com.csdn.swaggershirojwtredis.controller"))
.paths(Predicates.or(
PathSelectors.regex("/lamp.*"),
PathSelectors.regex("/lampState.*")
))
.build();
}
@Bean
public Docket LampState() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.groupName("获取Token")
.select()
.apis(RequestHandlerSelectors.basePackage("com.csdn.swaggershirojwtredis.controller"))
.paths(PathSelectors.ant("/GetToken"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("shiro-jwt-redis")
.description("api")
// 作者信息
.contact(new Contact("张川", "https://blog.csdn.net/qq_41595149", "[email protected]"))
.version("1.0.0")
.build();
}
}
application.properties文件添加是否开启swaager
#是否开启swagger
swagger.enabled=true
@EnableSwagger2 //开启swagger
org.apache.shiro
shiro-spring
1.3.2
package com.csdn.swaggershirojwtredis.config;
import com.csdn.swaggershirojwtredis.comment.JWTFilter;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.apache.shiro.mgt.SecurityManager;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;
/**
*@description: Shiro配置类
*
* @author: 张川
* @date: 2019-05-17 19:56
*
*/
@Configuration
public class ShiroConfig {
@Bean("securityManager")
public DefaultWebSecurityManager getManager(MyRealm realm) {
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
/*
* 关闭shiro自带的session,详情见文档
* http://shiro.apache.org/session-management.html#SessionManagement-StatelessApplications%28Sessionless%29
*/
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
manager.setSubjectDAO(subjectDAO);
// 使用自己的realm
manager.setRealm(realm);
return manager;
}
@Bean("shiroFilter")
public ShiroFilterFactoryBean factory(DefaultWebSecurityManager securityManager) {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
// 添加自己的过滤器并且取名为jwt
Map filterMap = new HashMap<>();
filterMap.put("jwt", new JWTFilter());
factoryBean.setFilters(filterMap);
factoryBean.setSecurityManager(securityManager);
factoryBean.setUnauthorizedUrl("/401");
/*
* 自定义url规则
* http://shiro.apache.org/web.html#urls-
*/
Map filterRuleMap = new HashMap<>();
// 所有请求通过我们自己的JWT Filter
filterRuleMap.put("/**", "jwt");
// 访问401和404页面不通过我们的Filter
filterRuleMap.put("/401", "anon");
factoryBean.setFilterChainDefinitionMap(filterRuleMap);
return factoryBean;
}
/**
* 下面的代码是添加注解支持
*/
/**
* 开启shiro aop注解支持.
* 使用代理方式;所以需要开启代码支持;
*/
@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
return new LifecycleBeanPostProcessor();
}
@Bean
@DependsOn({"lifecycleBeanPostProcessor"})
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
advisorAutoProxyCreator.setProxyTargetClass(true);
return advisorAutoProxyCreator;
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
}
package com.csdn.swaggershirojwtredis.config;
import com.csdn.swaggershirojwtredis.comment.JWTUtil;
import com.csdn.swaggershirojwtredis.comment.JwtToken;
import com.csdn.swaggershirojwtredis.comment.utils.RedisUtils;
import com.csdn.swaggershirojwtredis.entity.Module;
import com.csdn.swaggershirojwtredis.entity.Role;
import com.csdn.swaggershirojwtredis.entity.User;
import com.csdn.swaggershirojwtredis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* 自定义权限控制类
*
* @author keriezhang
* @Date: 2019-05-18 15:15
* @Description:
*/
@Slf4j
@Service
public class MyRealm extends AuthorizingRealm{
private UserService userService;
private RedisUtils redisUtils;
@Autowired
public MyRealm(UserService userService, RedisUtils redisUtils) {
super();
this.userService=userService;
this.redisUtils=redisUtils;
}
/**
* 大坑!,必须重写此方法,不然Shiro会报错
*/
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof JwtToken;
}
/**
* 认证.登录
* @param auth
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
String token = (String) auth.getCredentials();
// 解密获得username
String username = JWTUtil.getUsername(token);
if (username == null) {
throw new AuthenticationException("令牌无效");
}
User userBean = (User) redisUtils.get(token);
if (userBean == null) {
throw new AuthenticationException("令牌已过期");
} else {
redisUtils.expire(token, 60);
return new SimpleAuthenticationInfo(token, token, "MyRealm");
}
}
/**
* 授权
* @param principal
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
String token =(String)principal.getPrimaryPrincipal();
String username = JWTUtil.getUsername(token);
User user= userService.findUserByUserName(username);
List permissions=new ArrayList<>();
List rolesName=new ArrayList<>();
List roles = user.getRoles();
if(roles.size()>0) {
for(Role role : roles) {
rolesName.add(role.getRname());
List modules = role.getModules();
if(modules.size()>0) {
for(Module module : modules) {
permissions.add(module.getMname());
}
}
}
}
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//将角色放入shiro中
info.addRoles(rolesName);
//将权限放入shiro中
info.addStringPermissions(permissions);
return info;
}
}
com.auth0
java-jwt
3.2.0
package com.csdn.swaggershirojwtredis.comment;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Auther: 张川
* @Date: 2019-05-19 14:35
* @Description:
*/
@Slf4j
public class JWTFilter extends BasicHttpAuthenticationFilter {
/**
* 判断用户是否想要登入。
* 检测header里面是否包含token字段即可
*/
@Override
protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
HttpServletRequest req = (HttpServletRequest) request;
String authorization = req.getHeader("token");
return authorization != null;
}
/**
*
*/
@Override
protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String authorization = httpServletRequest.getHeader("token");
JwtToken token = new JwtToken(authorization);
// 提交给realm进行登入,如果错误他会抛出异常并被捕获
getSubject(request, response).login(token);
// 如果没有抛出异常则代表登入成功,返回true
return true;
}
/**
* 这里我们详细说明下为什么最终返回的都是true,即允许访问
* 例如我们提供一个地址 GET /article
* 登入用户和游客看到的内容是不同的
* 如果在这里返回了false,请求会被直接拦截,用户看不到任何东西
* 所以我们在这里返回true,Controller中可以通过 subject.isAuthenticated() 来判断用户是否登入
* 如果有些资源只有登入用户才能访问,我们只需要在方法上面加上 @RequiresAuthentication 注解即可
* 但是这样做有一个缺点,就是不能够对GET,POST等请求进行分别过滤鉴权(因为我们重写了官方的方法),但实际上对应用影响不大
*/
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
if (isLoginAttempt(request, response)) {
try {
executeLogin(request, response);
} catch (Exception e) {
System.out.println(e.getMessage());
request.setAttribute("msg",e.getMessage());
response401(request, response);
}
}
return true;
}
/**
* 对跨域提供支持
*/
@Override
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
// 跨域时会首先发送一个option请求,这里我们给option请求直接返回正常状态
if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
httpServletResponse.setStatus(HttpStatus.OK.value());
return false;
}
return super.preHandle(request, response);
}
/**
* 将非法请求跳转到 /401
*/
private void response401(ServletRequest req, ServletResponse resp) {
try {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
try {
request.getRequestDispatcher("/401").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
package com.csdn.swaggershirojwtredis.comment;
import org.apache.shiro.authc.AuthenticationToken;
/**
* @Auther: 张川
* @Date: 2019-05-19 14:12
* @Description:
*/
public class JwtToken implements AuthenticationToken {
private String token;
public JwtToken(String token) {
this.token = token;
}
@Override
public Object getPrincipal() {
return token;
}
@Override
public Object getCredentials() {
return token;
}
}
package com.csdn.swaggershirojwtredis.comment;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.io.UnsupportedEncodingException;
import java.util.Date;
/**
* @Auther: 张川
* @Date: 2019-05-19 14:15
* @Description:
*/
public class JWTUtil {
// 过期时间5分钟
private static final long EXPIRE_TIME =1000*60*5;
/**
* 校验token是否正确
* @param token 密钥
* @param password 用户的密码
* @return 是否正确
*/
public static boolean verify(String token, String username, String password) {
try {
Algorithm algorithm = Algorithm.HMAC256(password);
JWTVerifier verifier = JWT.require(algorithm)
.withClaim("username", username)
.build();
DecodedJWT jwt = verifier.verify(token);
return true;
} catch (Exception exception) {
return false;
}
}
/**
* 获得token中的信息无需password解密也能获得
* @return token中包含的用户名
*/
public static String getUsername(String token) {
try {
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("username").asString();
} catch (JWTDecodeException e) {
return null;
}
}
/**
* 生成签名,5min后过期
* @param username 用户名
* @param password 用户的密码
* @return 加密的token
*/
public static String sign(String username, String password) {
try {
Date date = new Date(System.currentTimeMillis()+EXPIRE_TIME);
Algorithm algorithm = Algorithm.HMAC256(password);
// 附带username信息
return JWT.create()
.withClaim( "username",username)
.withExpiresAt(date)
.sign(algorithm);
} catch (UnsupportedEncodingException e) {
return null;
}
}
}
package com.csdn.swaggershirojwtredis.comment;
/**
* @Auther: 张川
* @Date: 2019-05-19 14:16
* @Description:
*/
public class ResponseBean {
// http 状态码
private int code;
// 返回信息
private String msg;
// 返回的数据
private Object data;
public ResponseBean(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
package com.csdn.swaggershirojwtredis.comment;
/**
* @Auther: 张川
* @Date: 2019-05-19 14:17
* @Description:
*/
public class UnauthorizedException extends RuntimeException{
public UnauthorizedException(String msg) {
super(msg);
}
public UnauthorizedException() {
super();
}
}
org.springframework.boot
spring-boot-starter-data-redis
#Redis
# 缓存时长,单位秒
#cache.default-exp=100020
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 密码
spring.redis.password=
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=10000ms
# 连接池中的最大空闲连接,默认值也是8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接,默认值也是0
spring.redis.lettuce.pool.min-idle=0
# 如果赋值为-1,则表示不限制
spring.redis.lettuce.pool.max-wait=2000ms
package com.csdn.swaggershirojwtredis.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
public class FastJsonRedisSerializer implements RedisSerializer {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class clazz;
static {
ParserConfig.getGlobalInstance().addAccept("com.csdn.swaggershirojwtredis.entity");
}
public FastJsonRedisSerializer(Class clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (null == t) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (null == bytes || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
package com.csdn.swaggershirojwtredis.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* @Description:
* @author: 张川
* @date: 2019-05-24 13:01
*/
@Configuration
@ConditionalOnClass(RedisOperations.class) //系统中有RedisOperations类时
@EnableConfigurationProperties(RedisProperties.class) //启动RedisProperties这个类
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Autowired
RedisTemplate redisTemplate;
// 配置缓存管理器
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(60)) // 1分钟缓存失效
// 设置key的序列化方式
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
// 设置value的序列化方式
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new FastJsonRedisSerializer(Object.class)))
// 不缓存null值
.disableCachingNullValues();
RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.transactionAware()
.build();
return redisCacheManager;
}
}
package com.csdn.swaggershirojwtredis.comment.utils;
import com.csdn.swaggershirojwtredis.config.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @Description:redis工具类
* @author: 张川
* @date: 2019-05-24 14:05
*/
@Component
public class RedisUtils {
/**
* 注入redisTemplate bean
*/
private RedisTemplate redisTemplate;
@Autowired
public RedisUtils(RedisTemplate redisTemplate){
this.redisTemplate=redisTemplate;
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
this.redisTemplate.setValueSerializer(new FastJsonRedisSerializer(Object.class));
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
// ============================String(字符串)=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Hash(哈希)=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map
package com.csdn.swaggershirojwtredis.controller;
import com.csdn.swaggershirojwtredis.comment.JWTUtil;
import com.csdn.swaggershirojwtredis.comment.ResponseBean;
import com.csdn.swaggershirojwtredis.comment.utils.RedisUtils;
import com.csdn.swaggershirojwtredis.entity.User;
import com.csdn.swaggershirojwtredis.service.UserService;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* (User)表控制层
*
* @author 张川
* @since 2019-05-18 16:36:45
*/
@RestController
@Api(description = "获取Token")
public class UserController {
/**
* 服务对象
*/
@Resource
private UserService userService;
@Resource
private RedisUtils redisUtils;
/**
* 获取token
* @param username
* @param password
* @return
*/
@PostMapping("/GetToken")
public ResponseBean login(@RequestParam("username") String username, @RequestParam("password") String password) {
if(username==null){
return new ResponseBean(401,"请输入用户名","用户名不能为空");
}else if(password == null){
return new ResponseBean(401,"请输入密码","密码不能为空");
}else{
User userBean = userService.findUserByUserName(username);
if(userBean == null){
return new ResponseBean(401,"用户不存在","获取token失败");
}else if(userBean.getPassword()==null || !userBean.getPassword().equals(password)){
return new ResponseBean(401,"密码错误","获取token失败");
}else{
String token = JWTUtil.sign(username, password);
redisUtils.set(token,userBean,60);
return new ResponseBean(200, "OK", token);
}
}
}
@GetMapping(path = "/401")
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ResponseBean unauthorized(HttpServletRequest request) {
String data = (String) request.getAttribute("msg");
return new ResponseBean(401,"认证失败",data);
}
}
package com.csdn.swaggershirojwtredis.controller;
import com.csdn.swaggershirojwtredis.entity.Lampstate;
import com.csdn.swaggershirojwtredis.service.LampstateService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 灯状态信息(Lampstate)表控制层
*
* @author 张川
* @since 2019-05-17 16:14:51
*/
@RestController
@Api(description = "灯状态信息接口")
@RequestMapping("/lampState")
public class LampstateController {
/**
* 服务对象
*/
@Resource
private LampstateService lampstateService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@ApiOperation(value = "单个查询",notes = "根据id查询")
@ApiImplicitParams({
/**
*参数描述
*/
@ApiImplicitParam(paramType = "query",name = "id",dataType = "String",required = true,value = "唯一标识")
})
@ApiResponses({
@ApiResponse(code = 200,message = "成功"),
@ApiResponse(code=400,message = "请求参数没填好"),
@ApiResponse(code = 404,message = "请求路径没对")
})
@GetMapping("selectOne")
@RequiresRoles("admin")
public Lampstate selectOne(Integer id) {
return this.lampstateService.queryById(id);
}
}
1,
com.alibaba.fastjson.JSONException: autoType is not support. com.csdn.swaggershirojwtredis.entity.User
at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1026) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:316) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1356) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:45) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.JSON.parseObject(JSON.java:365) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269) ~[fastjson-1.2.47.jar:na]
at com.alibaba.fastjson.JSON.parseObject(JSON.java:488) ~[fastjson-1.2.47.jar:na]
at com.csdn.swaggershirojwtredis.config.FastJsonRedisSerializer.deserialize(FastJsonRedisSerializer.java:38) ~[classes/:na]
源码地址