-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
269 lines (238 loc) · 13 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://example.com</id>
<title></title>
<subtitle>不在能知,乃在能行</subtitle>
<icon>http://example.com/images/favicon.ico</icon>
<link href="http://example.com" />
<author>
<name>Dabing-He</name>
</author>
<updated>2024-03-15T13:05:35.901Z</updated>
<entry>
<id>http://example.com/%E6%9C%AB%E5%BD%B1%E7%9A%84%E6%AD%A6%E5%99%A8%E5%BA%93/</id>
<title>后端武器库</title>
<link rel="alternate" href="http://example.com/%E6%9C%AB%E5%BD%B1%E7%9A%84%E6%AD%A6%E5%99%A8%E5%BA%93/"/>
<content type="html"><p>额,what can I say, have funning!</p>
<p>&lt;h1&gt; 武器库 &lt;/h1&gt;</p>
<h2 id="处理器"><a class="anchor" href="#处理器">#</a> 处理器</h2>
<h4 id="1全局异常处理器"><a class="anchor" href="#1全局异常处理器">#</a> 1. 全局异常处理器</h4>
<p><img data-src="C:%5CUsers%5C%E6%9C%AB%E5%BD%B1%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240313232526145.png" alt="image-20240313232526145" /></p>
<pre><code class="language-java">@RestController
public class GlobalExceptionHandler&#123;
@ExceptionHandler(Exception.class)
public Result exceptionHandler(BaseException ex)&#123;
log.error(&quot;异常信息:&#123;&#125;&quot;, ex.getMessage());
return Result.error(ex.getMessage());
&#125;
&#125;
</code></pre>
<p><img data-src="C:%5CUsers%5C%E6%9C%AB%E5%BD%B1%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240313232545993.png" alt="image-20240313232545993" /></p>
<h3 id="2-登录拦截器"><a class="anchor" href="#2-登录拦截器">#</a> 2. 登录拦截器</h3>
<pre><code class="language-java">@Component
public class LoginInterceptor implements HandlerInterceptor &#123;
// @Autowired
// private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)&#123;
String token = request.getHeader(&quot;token&quot;);
try&#123;
//redis 获取相同token
// ValueOperations&lt;String,String&gt;operations=stringRedisTemplate.opsForValue();
// String redisToken = operations.get(token);
Object o = redisTemplate.opsForValue().get(token);
if(o==null)&#123;
throw new RuntimeException();
&#125;
// / if(redisToken== null)&#123;
// throw new RuntimeException();
// &#125;
Map&lt;String,Object&gt;claims= JwtUtil.parseToken(token);
ThreadLocalUtil.set(claims);
//放行
return true;
&#125;catch(Exception e)&#123;
response.setStatus(401);
return false;
&#125;
&#125;
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception &#123;
//清空ThreadLocal中的数据
ThreadLocalUtil.remove();
&#125;
&#125;
</code></pre>
<h2 id="工具类"><a class="anchor" href="#工具类">#</a> 工具类</h2>
<h4 id="threadlocal"><a class="anchor" href="#threadlocal">#</a> ThreadLocal</h4>
<p><img data-src="C:%5CUsers%5C%E6%9C%AB%E5%BD%B1%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240313232816691.png" alt="image-20240313232816691" /></p>
<pre><code class="language-java">public class BaseContext &#123;
public static ThreadLocal&lt;Long&gt; threadLocal = new ThreadLocal&lt;&gt;();
public static void setCurrentId(Long id) &#123;
threadLocal.set(id);
&#125;
public static Long getCurrentId() &#123;
return threadLocal.get();
&#125;
public static void removeCurrentId() &#123;
threadLocal.remove();
&#125;
&#125;
/////------------------------
/**
* ThreadLocal 工具类
*/
@SuppressWarnings(&quot;all&quot;)
public class ThreadLocalUtil &#123;
//提供ThreadLocal对象,
private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();
//根据键获取值
public static &lt;T&gt; T get()&#123;
return (T) THREAD_LOCAL.get();
&#125;
//存储键值对
public static void set(Object value)&#123;
THREAD_LOCAL.set(value);
&#125;
//清除ThreadLocal 防止内存泄漏
public static void remove()&#123;
THREAD_LOCAL.remove();
&#125;
&#125;
</code></pre>
<h4 id="result统一返回处理"><a class="anchor" href="#result统一返回处理">#</a> Result (统一返回处理)</h4>
<pre><code class="language-java">/**
* 后端统一返回结果
* @param &lt;T&gt;
*/
@Data
public class Result&lt;T&gt; implements Serializable &#123;
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
public static &lt;T&gt; Result&lt;T&gt; success() &#123;
Result&lt;T&gt; result = new Result&lt;T&gt;();
result.code = 1;
return result;
&#125;
public static &lt;T&gt; Result&lt;T&gt; success(T object) &#123;
Result&lt;T&gt; result = new Result&lt;T&gt;();
result.data = object;
result.code = 1;
return result;
&#125;
public static &lt;T&gt; Result&lt;T&gt; error(String msg) &#123;
Result result = new Result();
result.msg = msg;
result.code = 0;
return result;
&#125;
&#125;
</code></pre>
<h4 id="jwt令牌"><a class="anchor" href="#jwt令牌">#</a> JWT 令牌</h4>
<pre><code class="language-java">import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import java.util.Date;
import java.util.Map;
public class JwtUtil &#123;
private static final String KEY = &quot;moying&quot;;
//接收业务数据,生成token并返回
public static String genToken(Map&lt;String, Object&gt; claims) &#123;
return JWT.create()
.withClaim(&quot;claims&quot;, claims)
.withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 ))
.sign(Algorithm.HMAC256(KEY));
&#125;
//接收token,验证token,并返回业务数据
public static Map&lt;String, Object&gt; parseToken(String token) &#123;
return JWT.require(Algorithm.HMAC256(KEY))
.build()
.verify(token)
.getClaim(&quot;claims&quot;)
.asMap();
&#125;
&#125;
</code></pre>
<h2 id="配置类"><a class="anchor" href="#配置类">#</a> 配置类</h2>
<h4 id="redis序列化处理"><a class="anchor" href="#redis序列化处理">#</a> Redis (序列化处理)</h4>
<pre><code class="language-java">@Configuration
@Slf4j
public class RedisConfiguration&#123;
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory)&#123;
log.info(&quot;开始构建redis模板对象&quot;);
RedisTemplate redisTemplate=new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
&#125;
&#125;
</code></pre>
<h4 id="拦截器配置"><a class="anchor" href="#拦截器配置">#</a> 拦截器配置</h4>
<pre><code class="language-java">public class WebMvcConfiguration extends WebMvcConfigurationSupport &#123;
/**
* 注册自定义拦截器
*
* @param registry
*/
protected void addInterceptors(InterceptorRegistry registry) &#123;
log.info(&quot;开始注册自定义拦截器...&quot;);
registry.addInterceptor(jwtTokenAdminInterceptor)
.addPathPatterns(&quot;/user/**&quot;)
.excludePathPatterns(&quot;/user/login&quot;,&quot;/user/register&quot;);
&#125;
&#125;
</code></pre>
<h4 id="阿里oss配置"><a class="anchor" href="#阿里oss配置">#</a> 阿里 oss 配置</h4>
<pre><code class="language-java">@Configuration
@Slf4j
public class OssConfiguration &#123;
@Bean
public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties)
&#123;
log.info(&quot;开始创建阿里云文件上传工具类对象: &#123;&#125;&quot;,aliOssProperties);
return new AliOssUtil(aliOssProperties.getEndpoint(),
aliOssProperties.getAccessKeyId(),
aliOssProperties.getAccessKeySecret(),
aliOssProperties.getBucketName());
&#125;
&#125;
</code></pre>
</content>
<category term="抓哇" scheme="http://example.com/categories/%E6%8A%93%E5%93%87/" />
<category term="工具" scheme="http://example.com/categories/%E6%8A%93%E5%93%87/%E5%B7%A5%E5%85%B7/" />
<category term="cv工程" scheme="http://example.com/categories/%E6%8A%93%E5%93%87/%E5%B7%A5%E5%85%B7/cv%E5%B7%A5%E7%A8%8B/" />
<category term="Java" scheme="http://example.com/tags/Java/" />
<category term="JavaWeb" scheme="http://example.com/tags/JavaWeb/" />
<updated>2024-03-15T13:05:35.901Z</updated>
</entry>
<entry>
<id>http://example.com/hello-world/</id>
<title>Hello World</title>
<link rel="alternate" href="http://example.com/hello-world/"/>
<content type="html"><p>Welcome to <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvLw==">Hexo</span>! This is your very first post. Check <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mv">documentation</span> for more info. If you get any problems when using Hexo, you can find the answer in <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3MvdHJvdWJsZXNob290aW5nLmh0bWw=">troubleshooting</span> or you can ask me on <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hleG9qcy9oZXhvL2lzc3Vlcw==">GitHub</span>.</p>
<h2 id="quick-start"><a class="anchor" href="#quick-start">#</a> Quick Start</h2>
<h3 id="create-a-new-post"><a class="anchor" href="#create-a-new-post">#</a> Create a new post</h3>
<pre><code class="language-bash">$ hexo new &quot;My New Post&quot;
</code></pre>
<p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mvd3JpdGluZy5odG1s">Writing</span></p>
<h3 id="run-server"><a class="anchor" href="#run-server">#</a> Run server</h3>
<pre><code class="language-bash">$ hexo server
</code></pre>
<p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mvc2VydmVyLmh0bWw=">Server</span></p>
<h3 id="generate-static-files"><a class="anchor" href="#generate-static-files">#</a> Generate static files</h3>
<pre><code class="language-bash">$ hexo generate
</code></pre>
<p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3MvZ2VuZXJhdGluZy5odG1s">Generating</span></p>
<h3 id="deploy-to-remote-sites"><a class="anchor" href="#deploy-to-remote-sites">#</a> Deploy to remote sites</h3>
<pre><code class="language-bash">$ hexo deploy
</code></pre>
<p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mvb25lLWNvbW1hbmQtZGVwbG95bWVudC5odG1s">Deployment</span></p>
</content>
<updated>2024-03-15T10:47:51.197Z</updated>
</entry>
</feed>