V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ukipoi
V2EX  ›  Java

web 的项目如果所有的接口都是需要验证登陆状态的,有什么全局搞定的方法吗?

  •  
  •   ukipoi · 2018-10-23 13:26:12 +08:00 · 3732 次点击
    这是一个创建于 1983 天前的主题,其中的信息可能已经有所发展或是发生改变。

    AOP ?
    现在是写了一个方法,所有接口都调用一遍。

    33 条回复    2018-10-24 08:27:23 +08:00
    xinyewdz
        1
    xinyewdz  
       2018-10-23 13:31:27 +08:00
    拦截器,过滤器都可以。
    elgae
        2
    elgae  
       2018-10-23 13:31:38 +08:00
    filter
    lcy630409
        3
    lcy630409  
       2018-10-23 13:33:10 +08:00
    接口通过 ajax 或者 wss 传送,将他们封装一个函数,在里面判断...
    sutra
        4
    sutra  
       2018-10-23 13:38:28 +08:00
    不知道我题目有没有理解对,用 spring-security (当然其也是通过 filter )?
    gaius
        5
    gaius  
       2018-10-23 13:39:31 +08:00
    HandlerInterceptorAdapter
    ben1024
        6
    ben1024  
       2018-10-23 13:41:35 +08:00
    JWT,Header 验证
    wu67
        7
    wu67  
       2018-10-23 13:59:51 +08:00
    带 token, 没带 token 的请求全部报错就行了
    bankroft
        8
    bankroft  
       2018-10-23 14:01:29 +08:00 via Android
    decorate ?
    qwertyzzz
        9
    qwertyzzz  
       2018-10-23 15:21:24 +08:00
    继承?
    whypool
        10
    whypool  
       2018-10-23 15:41:29 +08:00
    中间件
    jason19659
        11
    jason19659  
       2018-10-23 15:46:22 +08:00
    filter interceptor
    KingEngine
        12
    KingEngine  
       2018-10-23 15:51:08 +08:00
    spring-security 嘛
    DesmondLiu90
        13
    DesmondLiu90  
       2018-10-23 16:14:10 +08:00
    authFilter 啊,里面写验证 token 的逻辑
    licoycn
        14
    licoycn  
       2018-10-23 16:24:38 +08:00
    过滤器
    licoycn
        15
    licoycn  
       2018-10-23 16:25:05 +08:00
    或者使用 Shiro 或 SpringSecurity 这样的安全框架
    yuanfnadi
        16
    yuanfnadi  
       2018-10-23 16:29:26 +08:00 via iPhone
    拦截器。
    liuxey
        17
    liuxey  
       2018-10-23 16:35:13 +08:00
    如果是普通 web servlet/jsp 项目,用:filter
    如果是 springMVC 项目,用:interceptor
    如果是 springMVC 大项目,用:SpringSecurity

    另不推荐使用 Shiro
    xuanbg
        18
    xuanbg  
       2018-10-23 16:40:55 +08:00
    非微服务项目:过滤器实现
    微服务项目:网关实现
    leeho
        19
    leeho  
       2018-10-23 16:47:32 +08:00
    axios 拦截器
    lllllliu
        20
    lllllliu  
       2018-10-23 16:49:02 +08:00
    AOP 可以。
    面向对象继承一个基础类,然后构造方法自动验证也可以。
    路由中间件也可以。
    akira
        21
    akira  
       2018-10-23 16:59:02 +08:00
    做一个前置网关处理更合理吧
    Variazioni
        22
    Variazioni  
       2018-10-23 17:22:22 +08:00
    还不如用 shiro 之类的安全框架。。配置更方便。。
    teg1c
        23
    teg1c  
       2018-10-23 17:35:16 +08:00
    中间件
    wobuhuicode
        24
    wobuhuicode  
       2018-10-23 17:36:58 +08:00
    JWT 啊
    98jiang
        25
    98jiang  
       2018-10-23 17:40:15 +08:00
    @liuxey 能问下为什么呀
    limuyan44
        26
    limuyan44  
       2018-10-23 17:40:59 +08:00 via Android
    filter。。几分钟写完,没什么复杂要求的话随便写写。。登录完 session 丢个 token
    tom
        27
    tom  
       2018-10-23 17:41:29 +08:00
    @liuxey #17 为啥不推荐 Shiro 啊?能说说原因吗?
    cnit
        28
    cnit  
       2018-10-23 19:46:19 +08:00
    cnit
        29
    cnit  
       2018-10-23 19:48:14 +08:00
    JokerZX
        30
    JokerZX  
       2018-10-23 19:49:37 +08:00
    同 jwt header 验证
    loveCoding
        31
    loveCoding  
       2018-10-23 20:05:03 +08:00
    @tom #27 shiro 也是 filter, 轻量级的自己写其实也不要多少时间
    Doodlister
        32
    Doodlister  
       2018-10-24 07:35:06 +08:00 via Android
    @liuxey shiro 咋啦
    liuxey
        33
    liuxey  
       2018-10-24 08:27:23 +08:00
    @tom
    @loveCoding
    @Doodlister

    可能我的想法比较主观,做个类比,想想现在的 struts
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5450 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:31 · PVG 16:31 · LAX 01:31 · JFK 04:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.