构建
使用Maven命令创建一个Maven项目
mvn archetype:generate:Maven插件原型是一个Maven项目模板工具包。
-DgroupId 包名
-DartifactId 项目名
-DarchetypeArtifactId 类型maven-archetype-quickstart,创建一个Java Project,maven-archetype-webapp,创建一个Web Project
-DinteractiveMode 是否使用交互模式,如果为false,非交互式的命令后直接创建,否则会有控制台提示输入操作
执行构建命令
mvn archetype:generate -DinteractiveMode=false -DgroupId=com.xcy -DartifactId=springboot -Dversion=1.0.0-SNAPSHOT
app.java
package com.xcy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName = "sessionFilter",urlPatterns = {"/*"})
class SessionFilter implements Filter {
//标示符:表示当前用户未登录(可根据自己项目需要改为json样式)
String NO_LOGIN = "您还未登录";
//不需要登录就可以访问的路径(比如:注册登录等)
String[] includeUrls = new String[]{"/login","register"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession(false);
String uri = request.getRequestURI();
System.out.println("filter url:"+uri);
//是否需要过滤
boolean needFilter = isNeedFilter(uri);
if (!needFilter) { //不需要过滤直接传给下一个过滤器
filterChain.doFilter(servletRequest, servletResponse);
} else { //需要过滤器
// session中包含user对象,则是登录状态
if(session!=null&&session.getAttribute("user") != null){
// System.out.println("user:"+session.getAttribute("user"));
filterChain.doFilter(request, response);
}else{
String requestType = request.getHeader("X-Requested-With");
//判断是否是ajax请求
if(requestType!=null && "XMLHttpRequest".equals(requestType)){
response.getWriter().write(this.NO_LOGIN);
}else{
//重定向到登录页(需要在static文件夹下建立此html文件)
response.sendRedirect(request.getContextPath()+"/user/login.html");
}
return;
}
}
}
/**
* @Author: xxxxx
* @Description: 是否需要过滤
* @Date: 2018-03-12 13:20:54
* @param uri
*/
public boolean isNeedFilter(String uri) {
for (String includeUrl : includeUrls) {
if(includeUrl.equals(uri)) {
return false;
}
}
return true;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
//@Controller
@RestController
class LoginController {
@RequestMapping("login")
public String login(String name, String pwd, HttpServletRequest request) {
HttpSession session = request.getSession();
if(name.equals("root")&&pwd.equals("root")) {
//User user = new User();
//user.setName(name);
session.setAttribute("user","root");
return "登录成功";
} else {
return "用户名或密码错误!";
}
}
}
@Configuration
class WebComponent2Config {
@Bean
public FilterRegistrationBean someFilterRegistration1() {
//新建过滤器注册类
FilterRegistrationBean registration = new FilterRegistrationBean();
// 添加我们写好的过滤器
registration.setFilter( new SessionFilter());
// 设置过滤器的URL模式
registration.addUrlPatterns("/*");
return registration;
}
}
@RestController
class first {
@GetMapping("hello")
public String index() {
return "HelloWorld~";
}
}
@SpringBootApplication
@ServletComponentScan
public class App
{
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}
// http://localhost:8080/login?name=root&pwd=root
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xcy</groupId>
<artifactId>springboot</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>springboot</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>