先简单说一下我们工程的架构:前端工程是采用react,后端工程采用spring-cloud,里面分为zuul工程和其他功能模块。zuul工程除了提供后端的路由转发,还可以做全局的过滤器,所以我选择在这个工程里面写登陆校验功能。
session配置
这里使用redis存储session信息。
下载依赖,在pom.xml里面加入
1 2 3 4 5 6 7 8 |
<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里面加入
1 2 3 4 5 6 7 8 9 |
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类,然后写入
1 2 3 4 5 6 7 8 9 10 11 |
package com.config;
import org.springframework.context.annotation.configuration;
@configuration public class redisconfig {
public redisconfig() {
} } |
过滤器
这里使用zuulfilter,实现了每个http请求都经过这个过滤器,然后通过session中是否存在用户名,判断session是否超时。如果超时就返回错误提示,前端再根据超时的http请求码进行跳转。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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 ; } } } |
这里还需要在启动类中注入这个类
1 2 3 4 |
@bean public websecurityfilter accessfilter() { return new websecurityfilter(); } |
登陆的代码
主要就是把前端传来的用户名密码放到session中,并进行校验。如果校验成功,返回登陆成功,否则,返回登陆失败。前端再根据登陆情况做路由跳转。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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); }
} |
最后,贴一下启动类的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@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); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://segmentfault.com/a/1190000017461035
查看更多关于springboot登陆过滤功能的实现代码的详细内容...