SpringSecurity四:Springboot集成SpringSecurity

13 篇文章 0 订阅

Spring security也是spring家族中的一员,使用spring boot集成spring security非常的方便,下面就通过一个例子来讲解一下如何在spring boot中集成spring security

      1. 创建mavean工程springboot_security

Pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
</parent>
<properties>
    <project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
    <!-- 以下是>spring boot依赖-->
   
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 以下是>spring security依赖-->
   
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>


    <!-- 以下是jsp依赖-->
   
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>

    </dependency>
    <!--jsp页面使用jstl标签 -->
   
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>

    </dependency>
    <!--用于编译jsp -->
   
<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>

    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.0</version>
    </dependency>
</dependencies>
<build>
    <finalName>security-springboot</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>src/main/java</directory>
                            <includes>
                                <include>**/*.xml</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

 

 

      1. 在resources下添加springboot配置文件,application.properties

server.port=8080

 

      1. 创建springboot启动类

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

 

      1. 创建springmvc配置类:

 

@Configuration

public class WebConfig implements WebMvcConfigurer {
   
@Override
   
public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(
"/").setViewName("redirect:/login");
    }
}

      1. 在application.properties中配置视图解析器的属性:

spring.mvc.view.prefix=/WEB‐INF/views/
spring.mvc.view.suffix=.jsp

 

      1. 创建spring security的配置文件

@Configuration
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter {
   
//配置用户信息服务
   
@Bean
   
public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager =
new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername(
"zhangsan").password("123").authorities("p1").build());
        manager.createUser(User.withUsername(
"lisi").password("456").authorities("p2").build());
       
return manager;
    }
   
@Bean
   
public PasswordEncoder passwordEncoder() {
        
return NoOpPasswordEncoder.getInstance();
    }
   
//配置安全拦截机制
   
@Override
   
protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(
"/r/r1").hasAuthority("p1")
                .antMatchers(
"/r/r2").hasAuthority("p2")
                .antMatchers(
"/r/**").authenticated()//所有/r/**的请求必须认证通过
               
.anyRequest().permitAll()//除了/r/**,其它的请求可以访问
               
.and()
                .formLogin()
//允许表单登录
                
.successForwardUrl("/login-success");//自定义登录成功的页面地址
   
}
}

 

      1. 创建控制器:LoginController

@RestController
public class LoginController {
   
@RequestMapping(value = "/login-success",produces = {"text/plain;charset=UTF-8"})
   
public String loginSuccess(){
       
return " 登录成功";
    }

   
/**
     *
测试资源1
     * @return
    
*/
   
@GetMapping(value = "/r/r1",produces = {"text/plain;charset=UTF-8"})
   
public String r1(){
       
return " 访问资源1";
    }

   
/**
     *
测试资源2
     * @return
    
*/
   
@GetMapping(value = "/r/r2",produces = {"text/plain;charset=UTF-8"})
   
public String r2(){
       
return " 访问资源2";
    }
}

测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaxiaomao1981

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值