Spring security Spring安全JWT令牌

Spring security Spring安全JWT令牌,spring-security,Spring Security,我使用Spring安全性来验证登录,我在securityContext.xml中配置Spring安全性 <http auto-config="false" use-expressions="true"> <intercept-url pattern="/**" access="hasRole('ROLE_DEV-TEAM')" /> <intercept-url pattern="/customerMgt/customers/admin/**" a

我使用Spring安全性来验证登录,我在securityContext.xml中配置Spring安全性

 <http auto-config="false" use-expressions="true">
    <intercept-url pattern="/**" access="hasRole('ROLE_DEV-TEAM')" />
    <intercept-url pattern="/customerMgt/customers/admin/**" access="hasRole('ROLE_ADD-COMPANY')" />

    <form-login login-page="/login" default-target-url="/main" always-use-default-target="true" authentication-failure-url="/login?error=1"/>
    <session-management />
    <logout logout-success-url="/login"/>
    <!--remember-me /--> <!-- AlexR: I did not have much luck with it in the past and IMO it presents security risk under strict DOD, etc. requirements -->
</http>

<beans:bean name="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

<authentication-manager>
    <ldap-authentication-provider
            user-search-filter="(uid={0})"
            user-search-base="ou=Users"
            group-search-filter="(uniqueMember={0})"
            group-search-base="ou=Groups"
            group-role-attribute="cn"
            role-prefix="ROLE_">

Ldap将被数据库取代

我不想在会话中设置令牌并重定向客户端,而是想创建一个带有令牌的自定义JWT并将其发送回客户端。Spring Security不支持令牌身份验证。然而,这是非常可行的。您需要提供自己的以下实现:

  • TokenAuthenticationFilter
    -为每个请求对请求头中提供的JWT令牌进行身份验证。用它替换基本的身份验证过滤器
  • LoginController
    -验证给定的凭据并在响应头中放置JWT令牌
  • JWTAuthenticationService
    -处理令牌创建、加密、解密和有效性检查。由上面两个使用

使用令牌身份验证时,您的安全上下文应该是无状态的,并且每个请求都由
TokenAuthenticationFilter
进行身份验证。因此,将http安全性配置为
无状态
会话管理和
http403禁止入口点

如何使用spring安全jjwt

顺便说一句,我是这个图书馆的作者。它提供满足您需求的JWT安全服务