Java后端开发——根据token写日志代码示例
Posted 不是公子的小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } }
如果觉得上述内容还可以的话,可以扫描下方二维码进行赞赏哟~
以上是关于Java后端开发——根据token写日志代码示例的主要内容,如果未能解决你的问题,请参考以下文章