Spring Boot Security 使用教程

虽然,我在实际项目中使用的是 shiro 进行权限管理,但 spring boot security 早已大名鼎鼎,虽然他的入门要相对复杂一点,但是设计视乎更加吸引人。

本章节就是以一篇快速入门 spring boot security 来实现如何使用 spring boot security,本示例来源于 Spring官方实例

  • java jdk1.8
  • maven 3.2+
  • spring boot 2.1.6

1 新建 Spring Boot Maven 示例工程项目

  1. File > New > Project,如下图选择 Spring Initializr 然后点击 【Next】下一步
  2. 填写 GroupId(包名)、Artifact(项目名) 即可。点击 下一步
    groupId=com.fishpro
    artifactId=securing
  3. 选择依赖 Spring Web Starter 前面打钩。
  4. 项目名设置为 spring-boot-study-securing.

2 导入依赖 Pom.xml

引入了包括

  • thymeleaf
  • spring-boot-starter-security


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.fishpro
    securing
    0.0.1-SNAPSHOT
    securing
    Demo project for Spring Boot

    
        1.8
    

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

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

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3 编写示例代码

3.1 WebSecurityConfig-编写配置文件

在配置文件 WebSecurityConfig 是哟合那个注解 @EnableWebSecurity,这样就能在 Web 的应用层使用了。

WebSecurityConfig 继承于 WebSecurityConfigurerAdapter 并重新了方法 configure 和 userDetailsService。

src/main/java/com/fishpro/securing/config/WebSecurityConfig.java

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);
    }
}

3.2 配置mvc

src/main/java/com/fishpro/securing/config/MvcConfig.java

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");
    }
}

3.3 前端 登录页面 /login

src/resources/templates/login.html




    Spring Security Example 


Invalid username and password.
You have been logged out.

3.4 前端 首页 /home

src/resources/templates/home.html




    Spring Security Example


Welcome!

Click here to see a greeting.

3.5 前端 认证成功页面 /hello

src/resources/templates/hello.html




    Hello World!


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

4 运行示例

右键 SecuringApplication 选择 Run SecuringApplication 后在浏览器中输入 http://localhost:8080

Spring Boot Security 使用教程_第1张图片

前往登录页面

Spring Boot Security 使用教程_第2张图片

输入错误的用户名和密码

Spring Boot Security 使用教程_第3张图片

用户名密码正确后的跳转

Spring Boot Security 使用教程_第4张图片

转载于:https://www.cnblogs.com/fishpro/p/spring-boot-study-securing.html

你可能感兴趣的:(Spring Boot Security 使用教程)