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>

      <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 测试

9 自定义登陆页面

  在resources下面定义webapp/WEB-INF/views/login.jsp    login.jsp的表单提价地址为login

  在Webconfig类中配置这个页面

      @Override

      public void addViewControlers(viewControllerRegistry registry){

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

        registry.addViewController("/login-view").setViewName("/login"); //login.jsp

      }

  在spring Security配置文件中配置登陆页面

  @Bean

  protected void configure(HttpSecurity http) throws Exception{

    http.authorizeRequests()

    .antMatchers("/r/**").authenticated()

    .anyRequest().permitAll()

    .and()

    .formLogin()

    .loginPage("/login-view")//addViewController("/login-view")

    .loginProcessingUel("/login")//表单提交url

    .sucessFowardUrl("/login-sucess") //controller的请求地址

    .csrf().disable();//屏蔽快站请求拦截  如果不想屏蔽可以在login.jsp中加一个input标签如下

  } 

      ==========================================================

      login.jsp

      <form>

        <input type="hiden" name="${_csrf.parameterName} " value="_csrf.token"/>

        ...............................

      </form>

 

10 获取用户登陆信息

  public String getUserName(){

    String userName=null;

    //当前认证通过的用户信息

    Authentication authentication = SecurityContextHolder.getContext().getAuthenitcation();

    //用户身份

    Object principal = authentication.getPrincipal();

    if(principal == null){

      userName = "匿名";

    }

    if(principal instanceof UserDetails){

      UserDetails userDetails = (UserDetails )principal ;

      userName = userDetails .getUserName();

    }else{

      userName = principal.toString();

    }

    return userName;

  }

11 会话管理

  http.sessionManagement()

    .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);//never statless ...

  如果是分布式采用状态认证  采用token生成令牌的方式进行。

  目前采用servlet的方式需要采用session的方式进行认证

 

  另外 会话的参数还有其他

  比如会话的超时配置server.servlet.session.timeout=3600s

  还有其他server.servlet.session.cookie.http-only=true

      server.servlet.session.secure=true

 

12 自定义退出

  http.logout()

    .logOutUrl("/logout")

    .logOutSucessUrl("/logout-view?logout");

  退出后默认HttpSession失效 清理SecurityContextHolder,跳转到/logout-view?logout

 

13 授权  分为web授权通过拦截url实现  以及方法授权通过controller或者service方法判断来实现

 

  web授权主要是操作httpSecurity的配置,比较简单。不做记录

  基于方法的授权一般用在Controller上面,提供很多注解进行标注。主要有三类

  @PreAuthorize     @PostAuthorize      @Secured

  使用前提是在任何一个config配置类上边添加一个注解

  @EnableGlobalMethodSecurity(secruredEnabled = true)

  @EnableGlobalMethodSecurity(prePostEnabled = true)

  然后再类或者接口上使用上面的三个注解就可以对方法进行权限控制。这些被标注的方法的属性会传递到AccessDesionManager

  进行投票。

  比如可以在一个controller上面增加注解@PreAuthorize (“hasAuthority(’P1‘)”)表示拥有P1权限就能访问。

 

posted @ 2020-02-08 21:20  工设091  阅读(366)  评论(0)    收藏  举报