Java后端开发——根据token写日志代码示例
@GetMapping("download/result")
@ApiOperation("提供手机号绑定结果给浏览器下载") //Controller层代码
public Response<Boolean> downloadResultExcel(HttpServletRequest request) throws IOException {
String token = request.getHeader("Authorization");
return excelService.downloadResultExcel(token);
}
// @GetMapping("download/result") // @ApiOperation("提供手机号绑定结果给浏览器下载") public Response<Boolean> downloadResultExcel(String token)throws IOException { //Service层代码 String userIdStr = this.redisService.get(token); //调用redisService取得当前页面用户信息 User user = this.userMapper.selectById(userIdStr); if (user==null || user.getIsDelete()==1){ //判断该用户是否存在 return new Response<>("导出手机号绑定信息日志写入失败"); }else { if (operationLogService.addOperationLog(user.getId(),"导出手机号绑定信息成功",1).getCode().equals("500")){ return new Response<>("导出手机号绑定信息日志写入失败"); } } return new Response<>("",Response.SUCCESS_CODE,Boolean.TRUE); } }
附上redisService代码如下:
@Autowired private StringRedisTemplate stringRedisTemplate; public String get(String key) { return this.stringRedisTemplate.opsForValue().get(key); }
附上StringRedisTemplate代码如下:
package org.springframework.data.redis.core; import org.springframework.data.redis.connection.DefaultStringRedisConnection; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; public class StringRedisTemplate extends RedisTemplate<String, String> { public StringRedisTemplate() { RedisSerializer<String> stringSerializer = new StringRedisSerializer(); this.setKeySerializer(stringSerializer); this.setValueSerializer(stringSerializer); this.setHashKeySerializer(stringSerializer); this.setHashValueSerializer(stringSerializer); } public StringRedisTemplate(RedisConnectionFactory connectionFactory) { this(); this.setConnectionFactory(connectionFactory); this.afterPropertiesSet(); } protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) { return new DefaultStringRedisConnection(connection); } }
扫码关注公众号,查看更多精彩内容
本文来自博客园,作者:不是公子的小白,转载请注明原文链接:https://www.cnblogs.com/bobbycheng/p/11866241.html