SpringSecurity之——SpringBoot整合SpringSecurity

一、创建工程

新建Maven工程springsecurity-demo,并编辑pom.xml文件如下:



    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
        
    

    4.0.0
    war
    springsecurity-demo
    springsecurity-demo

    
        UTF-8
        UTF-8
        1.8
    

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

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.0
                
                    1.8
                    1.8
                
            
        
    

二、实现代码

1.创建启动类
在项目的src/main/java下创建io.binghe.demo包,并在包下创建DemoApplication类,代码如下:

package io.binghe.demo;

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

/**
 * @author binghe
 * @version 1.0.0
 * @description 项目启动类
 */
@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication {
    public static void main(String[] args){
        SpringApplication.run(DemoApplication.class, args);
    }
}

2.在io.binghe.demo包下创建ServletInitializer类,代码如下:

package io.binghe.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * @author binghe
 * @version 1.0.0
 * @description Servlet初始化类
 */
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
}

3.在io.binghe.demo包下创建SpringSecurityConfig类,同于配置SpringSecurity,代码如下:

package io.binghe.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author binghe
 * @version 1.0.0
 * @description SpringSecurity配置类
 */
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
                .logout().permitAll()
                .and()
                .formLogin();
        http.csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
    }
}

4.创建controller类,新建io.binghe.demo.controller包,在包下创建DemoController类,代码如下:

package io.binghe.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author binghe
 * @version 1.0.0
 * @description 测试使用的Controller
 */
@RestController
public class DemoController {

    @RequestMapping(value = "/")
    public String home(){
        return "hello string boot";
    }

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello world";
    }
}

三、测试

1.启动工程
运行DemoApplication的main方法

2.测试工程
在浏览器分别访问http://localhost:8080和http://localhost:8080/hello。可以看到可以直接访问http://localhost:8080,而访问http://localhost:8080/hello则需要输入用户名和密码。说明SpringBoot整合SpringSecurity成功。

附图如下:

访问http://localhost:8080效果如下:

SpringSecurity之——SpringBoot整合SpringSecurity_第1张图片

访问http://localhost:8080/hello效果如下:

SpringSecurity之——SpringBoot整合SpringSecurity_第2张图片

 

你可能感兴趣的:(SpringSecurity,权限管理,SpringBoot)