Skip to content

Commit

Permalink
add inteceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaxinyuuu committed Dec 14, 2021
1 parent 381bf0f commit e9b882c
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 17 deletions.
10 changes: 3 additions & 7 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions log/MyBlog-dev.log
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,67 @@
2021-12-14 15:17:20.463 INFO 18725 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-12-14 15:17:20.463 INFO 18725 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-12-14 15:17:20.464 INFO 18725 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2021-12-14 15:20:30.449 INFO 18725 --- [SpringApplicationShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-12-14 15:20:30.452 INFO 18725 --- [SpringApplicationShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-12-14 15:20:30.461 INFO 18725 --- [SpringApplicationShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2021-12-14 15:24:18.830 INFO 18838 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : Starting MyBlogApplication using Java 1.8.0_312 on xiaxinyudeMBP with PID 18838 (/Users/xiaxinyu/Desktop/github-repo/MyBlog/target/classes started by xiaxinyu in /Users/xiaxinyu/Desktop/github-repo/MyBlog)
2021-12-14 15:24:18.831 DEBUG 18838 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : Running with Spring Boot v2.6.1, Spring v5.3.13
2021-12-14 15:24:18.831 INFO 18838 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : The following profiles are active: dev
2021-12-14 15:24:18.882 INFO 18838 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-12-14 15:24:18.882 INFO 18838 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-12-14 15:24:19.844 INFO 18838 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-12-14 15:24:19.901 INFO 18838 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 48 ms. Found 1 JPA repository interfaces.
2021-12-14 15:24:21.037 INFO 18838 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-12-14 15:24:21.057 INFO 18838 --- [restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-12-14 15:24:21.058 INFO 18838 --- [restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-12-14 15:24:21.180 INFO 18838 --- [restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-12-14 15:24:21.180 INFO 18838 --- [restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2297 ms
2021-12-14 15:24:21.574 INFO 18838 --- [restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-12-14 15:24:21.660 INFO 18838 --- [restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-12-14 15:24:22.001 INFO 18838 --- [restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-12-14 15:24:22.120 INFO 18838 --- [restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-12-14 15:24:22.122 WARN 18838 --- [restartedMain] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2021-12-14 15:24:22.508 INFO 18838 --- [restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-12-14 15:24:22.533 INFO 18838 --- [restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2021-12-14 15:24:23.259 INFO 18838 --- [restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-12-14 15:24:23.269 INFO 18838 --- [restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-12-14 15:24:23.860 WARN 18838 --- [restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-12-14 15:24:24.137 INFO 18838 --- [restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2021-12-14 15:24:24.431 INFO 18838 --- [restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-12-14 15:24:24.648 INFO 18838 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-12-14 15:24:24.658 INFO 18838 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : Started MyBlogApplication in 6.248 seconds (JVM running for 7.197)
2021-12-14 15:24:30.081 INFO 18838 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-12-14 15:24:30.082 INFO 18838 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-12-14 15:24:30.083 INFO 18838 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2021-12-14 15:25:21.258 INFO 18838 --- [SpringApplicationShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-12-14 15:25:21.260 INFO 18838 --- [SpringApplicationShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-12-14 15:25:21.269 INFO 18838 --- [SpringApplicationShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2021-12-14 15:43:25.214 INFO 19074 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : Starting MyBlogApplication using Java 1.8.0_312 on xiaxinyudeMBP with PID 19074 (/Users/xiaxinyu/Desktop/github-repo/MyBlog/target/classes started by xiaxinyu in /Users/xiaxinyu/Desktop/github-repo/MyBlog)
2021-12-14 15:43:25.216 DEBUG 19074 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : Running with Spring Boot v2.6.1, Spring v5.3.13
2021-12-14 15:43:25.216 INFO 19074 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : The following profiles are active: dev
2021-12-14 15:43:25.267 INFO 19074 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-12-14 15:43:25.268 INFO 19074 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-12-14 15:43:26.129 INFO 19074 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-12-14 15:43:26.174 INFO 19074 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 38 ms. Found 1 JPA repository interfaces.
2021-12-14 15:43:27.048 INFO 19074 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-12-14 15:43:27.060 INFO 19074 --- [restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-12-14 15:43:27.060 INFO 19074 --- [restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-12-14 15:43:27.130 INFO 19074 --- [restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-12-14 15:43:27.131 INFO 19074 --- [restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1862 ms
2021-12-14 15:43:27.351 INFO 19074 --- [restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-12-14 15:43:27.401 INFO 19074 --- [restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-12-14 15:43:27.604 INFO 19074 --- [restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-12-14 15:43:27.711 INFO 19074 --- [restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-12-14 15:43:27.713 WARN 19074 --- [restartedMain] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2021-12-14 15:43:28.009 INFO 19074 --- [restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-12-14 15:43:28.029 INFO 19074 --- [restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2021-12-14 15:43:28.946 INFO 19074 --- [restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-12-14 15:43:28.959 INFO 19074 --- [restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-12-14 15:43:29.744 WARN 19074 --- [restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-12-14 15:43:30.082 INFO 19074 --- [restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2021-12-14 15:43:30.650 INFO 19074 --- [restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-12-14 15:43:30.703 INFO 19074 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-12-14 15:43:30.730 INFO 19074 --- [restartedMain] com.xiaxinyu.myblog.MyBlogApplication : Started MyBlogApplication in 5.872 seconds (JVM running for 6.555)
2021-12-14 15:43:37.136 INFO 19074 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-12-14 15:43:37.137 INFO 19074 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-12-14 15:43:37.138 INFO 19074 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.xiaxinyu.myblog.interceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
if (request.getSession().getAttribute("user") == null) {
response.sendRedirect("/admin");
return false;
}
return true;
}
}


19 changes: 19 additions & 0 deletions src/main/java/com/xiaxinyu/myblog/interceptor/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.xiaxinyu.myblog.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/admin/**") //想拦截的路径
.excludePathPatterns("/admin") //不想拦截的路径
.excludePathPatterns("/admin/login");
}
}


14 changes: 14 additions & 0 deletions src/main/java/com/xiaxinyu/myblog/web/admin/BlogController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xiaxinyu.myblog.web.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class BlogController {
@GetMapping("/blogs")
public String showBlogs(){
return "admin/blogs";
}
}
14 changes: 9 additions & 5 deletions src/main/resources/templates/admin/blogs.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
<meta charset="UTF-8">
<!-- 设置移动端预览-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>博客管理</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
<link rel="stylesheet" href="../../static/css/css/me.css">
<link rel="stylesheet" href="../../static/css/me.css">
</head>

<body>

<!--导航栏-->
<nav class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
<nav th:replace="admin/_fragments :: menu(1)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
<!-- attached 去除圆角-->
<!-- inverted 颜色反转-->
<div class="ui container">
Expand Down Expand Up @@ -137,7 +138,7 @@ <h2 class="ui teal header item">博客后台</h2>
<br>

<!--底部footer-->
<footer class="ui inverted attached segment m-padded-tb-massive">
<footer th:replace="admin/_fragments :: footer" class="ui inverted attached segment m-padded-tb-massive">
<div class="ui center aligned container">
<!-- center aligned使container内的内容居中 -->
<div class="ui inverted divided stackable grid">
Expand Down Expand Up @@ -177,8 +178,11 @@ <h4 class="ui inverted header">My blog</h4>

</footer>

<!--/*/<th:block th:replace="_fragments :: script">/*/-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<!--/*/</th:block>/*/-->

<script>
$('.menu.toggle').click(function () {
$(".m-item").toggleClass('m-mobile-hide');
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 9 additions & 5 deletions target/classes/templates/admin/blogs.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
<meta charset="UTF-8">
<!-- 设置移动端预览-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>博客管理</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
<link rel="stylesheet" href="../../static/css/css/me.css">
<link rel="stylesheet" href="../../static/css/me.css">
</head>

<body>

<!--导航栏-->
<nav class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
<nav th:replace="admin/_fragments :: menu(1)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
<!-- attached 去除圆角-->
<!-- inverted 颜色反转-->
<div class="ui container">
Expand Down Expand Up @@ -137,7 +138,7 @@ <h2 class="ui teal header item">博客后台</h2>
<br>

<!--底部footer-->
<footer class="ui inverted attached segment m-padded-tb-massive">
<footer th:replace="admin/_fragments :: footer" class="ui inverted attached segment m-padded-tb-massive">
<div class="ui center aligned container">
<!-- center aligned使container内的内容居中 -->
<div class="ui inverted divided stackable grid">
Expand Down Expand Up @@ -177,8 +178,11 @@ <h4 class="ui inverted header">My blog</h4>

</footer>

<!--/*/<th:block th:replace="_fragments :: script">/*/-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<!--/*/</th:block>/*/-->

<script>
$('.menu.toggle').click(function () {
$(".m-item").toggleClass('m-mobile-hide');
Expand Down

0 comments on commit e9b882c

Please sign in to comment.