這篇文章帶給大家的內容是關於spring-boot登陸過濾功能,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
先簡單說一下我們工程的架構:前端工程是採用react,後端工程採用spring-cloud,裡面分為zuul工程和其他功能模組。 zuul工程除了提供後端的路由轉發,還可以做全域的過濾器,所以我選擇在這個工程裡面寫登陸校驗功能。
這裡使用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); } }
以上是spring-boot的登陸過濾功能的實現代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!