Skip to content

Commit

Permalink
🎉 init commit personal blog codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHENFENG13 committed Mar 4, 2019
0 parents commit d1fea47
Show file tree
Hide file tree
Showing 68 changed files with 5,643 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
personal-blog
125 changes: 125 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- @author 13
@qq交流群 796794009
@email [email protected]
@link http:13blog.site -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.site.blog.my.core</groupId>
<artifactId>my-blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>my-blog</name>
<description>your personal blog</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
<!-- commonmark core -->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.8.0</version>
</dependency>
<!-- commonmark table -->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-gfm-tables</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId> mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version> 5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- MybatisGenerator的配置文件位置 -->
<configurationFile>src/main/resources/mybatisGeneratorConfig.xml</configurationFile>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- @author 13
@qq交流群 796794009
@email [email protected]
@link http:13blog.site
-->
</project>
19 changes: 19 additions & 0 deletions src/main/java/com/site/blog/my/core/MyBlogApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.site.blog.my.core;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author 13
* @qq交流群 796794009
* @email [email protected]
* @link http:13blog.site
*/
@MapperScan("com.site.blog.my.core.dao")
@SpringBootApplication
public class MyBlogApplication {
public static void main(String[] args) {
SpringApplication.run(MyBlogApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.site.blog.my.core.config;

import com.site.blog.my.core.interceptor.AdminLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyBlogWebMvcConfigurer implements WebMvcConfigurer {

@Autowired
private AdminLoginInterceptor adminLoginInterceptor;

public void addInterceptors(InterceptorRegistry registry) {
// 添加一个拦截器,拦截以/admin为前缀的url路径
registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/login").excludePathPatterns("/admin/dist/**").excludePathPatterns("/admin/plugins/**");
}

/**
* 资源映射
* @param registry
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/upload/**").addResourceLocations("file:/opt/my-blog/upload/");
registry.addResourceHandler("/md-upload/**").addResourceLocations("file:/opt/my-blog/md-upload/");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.site.blog.my.core.controller.admin;

import com.site.blog.my.core.entity.AdminUser;
import com.site.blog.my.core.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
* @author 13
* @qq交流群 796794009
* @email [email protected]
* @link http:13blog.site
*/
@Controller
@RequestMapping("/admin")
public class AdminController {

@Resource
private AdminUserService adminUserService;
@Resource
private BlogService blogService;
@Resource
private CategoryService categoryService;
@Resource
private LinkService linkService;
@Resource
private TagService tagService;
@Resource
private CommentService commentService;


@GetMapping({"/login"})
public String login() {
return "admin/login";
}

@GetMapping({"/test"})
public String test() {
return "admin/test";
}


@GetMapping({"", "/", "/index", "/index.html"})
public String index(HttpServletRequest request) {
request.setAttribute("path", "index");
request.setAttribute("categoryCount", categoryService.getTotalCategories());
request.setAttribute("blogCount", blogService.getTotalBlogs());
request.setAttribute("linkCount", linkService.getTotalLinks());
request.setAttribute("tagCount", tagService.getTotalTags());
request.setAttribute("commentCount", commentService.getTotalComments());
request.setAttribute("path", "index");
return "admin/index";
}

@PostMapping(value = "/login")
public String login(@RequestParam("userName") String userName,
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session过期时间设置为7200秒 即两小时
//session.setMaxInactiveInterval(60 * 60 * 2);
return "redirect:/admin/index";
} else {
session.setAttribute("errorMsg", "登陆失败");
return "admin/login";
}
}

@GetMapping("/profile")
public String profile(HttpServletRequest request) {
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
AdminUser adminUser = adminUserService.getUserDetailById(loginUserId);
if (adminUser == null) {
return "admin/login";
}
request.setAttribute("path", "profile");
request.setAttribute("loginUserName", adminUser.getLoginUserName());
request.setAttribute("nickName", adminUser.getNickName());
return "admin/profile";
}

@PostMapping("/profile/password")
@ResponseBody
public String passwordUpdate(HttpServletRequest request, @RequestParam("originalPassword") String originalPassword,
@RequestParam("newPassword") String newPassword) {
if (StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(newPassword)) {
return "参数不能为空";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updatePassword(loginUserId, originalPassword, newPassword)) {
//修改成功后清空session中的数据,前端控制跳转至登录页
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "success";
} else {
return "修改失败";
}
}

@PostMapping("/profile/name")
@ResponseBody
public String nameUpdate(HttpServletRequest request, @RequestParam("loginUserName") String loginUserName,
@RequestParam("nickName") String nickName) {
if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
return "参数不能为空";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
return "success";
} else {
return "修改失败";
}
}

@GetMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "admin/login";
}
}
Loading

0 comments on commit d1fea47

Please sign in to comment.