先簡單說一下咱們工程的架構:前端工程是採用react,後端工程採用spring-cloud,裏面分爲zuul工程和其餘功能模塊。zuul工程除了提供後端的路由轉發,還能夠作全局的過濾器,因此我選擇在這個工程裏面寫登錄校驗功能。前端
這裏使用redis存儲session信息。
下載依賴,在pom.xml裏面加入java
<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裏面加入react
session: store-type: redis timeout: 30 redis: database: 0 host: password: port: 6379 timeout: 300000
session超時,在application.yml裏面配置timeout貌似沒有效果,咱們在啓動類裏面加入超時配置註解redis
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 7200, redisFlushMode = RedisFlushMode.IMMEDIATE)
添加redis類配置,新建一個redisConfig類,而後寫入spring
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); //queryString.indexOf("tag=process") > -1)是爲了外系統調用這個系統跳過登錄約定傳過來的值 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; } } }
這裏還須要在啓動類中注入這個類session
@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); } }
最後,貼一下啓動類的代碼app
@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); } }