Springboot項目的簡單

接着我們的項目來:
首先是個pox:
pom.xml:`
4.0.0

<groupId>com.github.carter659</groupId>
<artifactId>spring13</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<name>spring13</name>
<url>http://maven.apache.org</url>

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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
其次是app.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

 *
 */
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

具體實現`

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
*
*
*/
@Controller
public class MainController {

@GetMapping("/")
public String index(@SessionAttribute(WebSecurityConfig.SESSION_KEY) String account, Model model) {
    model.addAttribute("name", account);
    return "index";
}

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

@PostMapping("/loginPost")
public @ResponseBody Map<String, Object> loginPost(String account, String password, HttpSession session) {
    Map<String, Object> map = new HashMap<>();
    if (!"123456".equals(password)) {
        map.put("success", false);
        map.put("message", "密碼錯誤");
        return map;
    }

    // 設置session
    session.setAttribute(WebSecurityConfig.SESSION_KEY, account);

    map.put("success", true);
    map.put("message", "登錄成功");
    return map;
}

@GetMapping("/logout")
public String logout(HttpSession session) {
    // 移除session
    session.removeAttribute(WebSecurityConfig.SESSION_KEY);
    return "redirect:/login";
}

講解MainController:

這裏的四個方法分別是:登錄後的頁面、登錄頁面、登錄ajax後臺方法和註銷。

“loginPost”方法判斷當密碼爲“123456”時則設置session

“index”方法用來顯示session

“logout”方法用來移除session

新建“WebSecurityConfig”類文件

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 
 *
 */
@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter {

    /**
     * 登錄session key
     */
    public final static String SESSION_KEY = "user";

    @Bean
    public SecurityInterceptor getSecurityInterceptor() {
        return new SecurityInterceptor();
    }

    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());

        // 排除配置
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/login**");

        // 攔截配置
        addInterceptor.addPathPatterns("/**");
    }

    private class SecurityInterceptor extends HandlerInterceptorAdapter {

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            HttpSession session = request.getSession();
            if (session.getAttribute(SESSION_KEY) != null)
                return true;

            // 跳轉登錄
            String url = "/login";
            response.sendRedirect(url);
            return false;
        }
    }
}

頁面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>spring boot——簡單登錄認證</title>
</head>
<body>
    <h1>spring boot——簡單登錄認證</h1>
     <h3 th:text="'登錄用戶:' + ${name}"></h3>
    
    <a href="/logout">註銷</a>
    <br />
  
</body>
</html>
```<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>spring boot——簡單登錄認證</title>
</head>
<body>
    <h1>spring boot——簡單登錄認證</h1>
 
    <h3 th:text="'登錄用戶:' + ${name}"></h3>
    
    <a href="/logout">註銷</a>
    <br />
    
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章