亲宝软件园·资讯

展开

springboot 登陆过滤 springboot登陆过滤功能的实现代码

supportlss 人气:0

先简单说一下我们工程的架构:前端工程是采用react,后端工程采用spring-cloud,里面分为zuul工程和其他功能模块。zuul工程除了提供后端的路由转发,还可以做全局的过滤器,所以我选择在这个工程里面写登陆校验功能。

session配置

这里使用redis存储session信息。

下载依赖,在pom.xml里面加入

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
</dependency>

配置session存储,在application.yml里面加入

session:
  store-type: redis
  timeout: 30
redis:
  database: 0
  host: 
  password: 
  port: 6379
  timeout: 300000

session超时,在application.yml里面配置timeout貌似没有效果,我们在启动类里面加入超时配置注解

复制代码 代码如下:
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 7200, redisFlushMode = RedisFlushMode.IMMEDIATE)

添加redis类配置,新建一个redisConfig类,然后写入

package com.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class RedisConfig {

  public RedisConfig() {

  }
}

过滤器

这里使用zuulFilter,实现了每个http请求都经过这个过滤器,然后通过session中是否存在用户名,判断session是否超时。如果超时就返回错误提示,前端再根据超时的http请求码进行跳转。

package com.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.ztesoft.cloud.Service.StaffService;
import com.ztesoft.cloud.model.User;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class WebSecurityFilter extends ZuulFilter {

  @Override
  public String filterType() {
    return "pre";//前置过滤器
  }

  @Override
  public int filterOrder() {
    return 0;//优先级为0,数字越大,优先级越低
  }

  @Override
  public boolean shouldFilter() {
    return true;//是否执行该过滤器,此处为true,说明需要过滤
  }


  @Autowired
  private StaffService staffService;

  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    String queryString = request.getQueryString();
    Object username = request.getSession().getAttribute("username");
    Object password = request.getSession().getAttribute("password");
    User user = new User();
    if(username != null) {
      user.setUsername(username.toString());
    }
    if(password != null) {
      user.setPassword(password.toString());
    }
    Boolean verifyResult = this.staffService.verifyLoginUser(user);
    if ((queryString != null && queryString.indexOf("tag=process") > -1) || verifyResult) {
      ctx.setSendZuulResponse(true);// 对该请求进行路由
      ctx.setResponseStatusCode(200);
      ctx.set("isSuccess", true);// 设值,可以在多个过滤器时使用
      return null;
    } else {
      ctx.setSendZuulResponse(false);// 过滤该请求,不对其进行路由
      ctx.setResponseStatusCode(401);// 返回错误码,应该是401
      ctx.setResponseBody("session is out of time");// 返回错误内容
      ctx.set("isSuccess", false);
      return null;
    }
  }
}

这里还需要在启动类中注入这个类

  @Bean
  public WebSecurityFilter accessFilter() {
    return new WebSecurityFilter();
  }

登陆的代码

主要就是把前端传来的用户名密码放到session中,并进行校验。如果校验成功,返回登陆成功,否则,返回登陆失败。前端再根据登陆情况做路由跳转。

package com.controller;

@RestController
@RequestMapping(value = "/system")
public class SystemController extends JdkSerializationRedisSerializer implements Serializable {

  @Autowired
  private StaffService staffService;

  @PostMapping("login")
  public ResponseEntity<String> login(@RequestBody User user, HttpSession session) {
    session.setAttribute("username", user.getUsername());
    session.setAttribute("password", user.getPassword());
    Boolean ret = this.staffService.verifyLoginUser(user);
    if(ret == true) {
      return new ResponseEntity<>("登陆成功", HttpStatus.OK);
    }
    return new ResponseEntity<>("登陆失败", HttpStatus.NON_AUTHORITATIVE_INFORMATION);
  }

}

最后,贴一下启动类的代码

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 7200, redisFlushMode = RedisFlushMode.IMMEDIATE)
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = RibbonConfig.class))
@RibbonClients(defaultConfiguration = RibbonConfig.class)
public class PlatformWebApplication {

  @Bean
  public WebSecurityFilter accessFilter() {
    return new WebSecurityFilter();
  }

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

加载全部内容

相关教程
猜你喜欢
用户评论