spring Security项目与spring boot整合项目搭建

1 创建maven工程。

2 添加依赖。添加依赖和上一篇添加的依赖差不多。只需要做一下修改。

  1)将<packaging>var</packaging> 去掉

  2)添加parent依赖

    <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.1.3.RELEASE</version>

    </parent>

  3)集成spring boot的依赖,spring security等starter

    <dependency>

扫描二维码关注公众号,回复: 9019408 查看本文章

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-security</artifactId>

    </dependency>

3 配置spring容器

  在resoruces下面创建名为application.properties的配置文件,配置以下选项

    server.port=8080

    server.servlet.context-path=/security-boot

    spring.application.name=security-boot

4 创建启动类

5 在启动类所在包创建子包config包

  1)加载spring 容器的ApplicationConfig类不需要了。

  2)WebConfig类也就是配置servletContext的类需要,并且做以下修改

    》不需要扫描包的注解以及@EnableWebMvc的注解

    》视图解析器的Bean也不需要

      在配置文件application.properties中配置

        spring.mvc.view.prefix=/WEB-INF/views

        spring.mvc.view.suffix=.jsp

    》保留登陆URL的根路径

      @Override

      public void addViewControlers(viewControllerRegistry registry){

        //意思是访问“/”,就会跳转到 login.jsp

        registry.addViewController("/").setViewName("redirect:/login");

      }

  3) 安全配置WebSecurityConfig的类需要保留,做一下修改

    》将类注解修改为一个@configuration

6 controller还是需要

7 init包下的都不需要了。

8 测试

猜你喜欢

转载自www.cnblogs.com/dengw125792/p/12285179.html