springboot教程三:Securing a Web Application

该教程展示用户登录才能访问的网页

本系列教程使用idea完成
idea安装springboot插件


image.png

需求:
jdk1.8及以上
maven3.2+
如果添加的新库无法加载请参照idea maven 自定义配置这篇文章

第一步
新建项目


image.png

第二步
选择Spring Web 和 Thymeleaf 点击完成


image.png

第三步
创建home.html文件
src/main/resources/templates/home.html


    
        Spring Security Example
    
    
        

Welcome!

Click here to see a greeting.

第四步
创建src/main/resources/templates/hello.html文件



    
        Hello World!
    
    
        

Hello [[${#httpServletRequest.remoteUser}]]!

第五步
在pom.xml文件里面添加依赖


  org.springframework.boot
  spring-boot-starter-security


  org.springframework.security
  spring-security-test
  test

第六步
创建src/main/java/com/example/securingweb/MvcConfig.java文件

package com.example.securingweb;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }

}

第七步
创建src/main/java/com/example/securingweb/WebSecurityConfig.java文件

package com.example.securingweb;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user =
             User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}

第八步
创建src/main/resources/templates/login.html文件



    
        Spring Security Example 
    
    
        
Invalid username and password.
You have been logged out.

运行程序
在浏览器访问http://localhost:8080/hello
登录用户名user 密码password

你可能感兴趣的:(springboot教程三:Securing a Web Application)