Spring Security JWT

Source: Internet
Author: User

When the login is successful, return to a authentication request header, the next time the user request, only need to attach the request header, you can directly access the resources.

Pom.xml

<dependency>    <groupId>io.jsonwebtoken</groupId>    <artifactid>jjwt</ Artifactid>    <version>0.7.0</version></dependency>

Write a filter

@Component @slf4j Public classJwtauthenticationtokenfilterextendsOnceperrequestfilter {Private Static FinalString Application_json = "Application/json;charset=utf-8"; @AutowiredPrivateUserdetailsservice Userdetailsservice; @AutowiredPrivateJwttokenutil Jwttokenutil; @AutowiredPrivatejwtproperties jwtproperties; @Overrideprotected voidDofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain chain)throwsservletexception, IOException {String AuthToken=Request.getheader (Jwtproperties.getheader ()); if(!Stringutils.isempty (AuthToken))            {Jwttoken Jwttoken; Try{Jwttoken=Jwttokenutil.getjwttoken (AuthToken); String username=Jwttoken.getusername (); if(Username! =NULL&& Securitycontextholder.getcontext (). getauthentication () = =NULL) {userdetails userdetails=Userdetailsservice.loaduserbyusername (username); if(Jwttokenutil.validatetoken (AuthToken, userdetails)) {Usernamepasswordauthenticationtoken aut Hentication=NewUsernamepasswordauthenticationtoken (Userdetails,NULL, Userdetails.getauthorities ()); Authentication.setdetails (NewWebauthenticationdetailssource (). Builddetails (request));                        Securitycontextholder.getcontext (). Setauthentication (authentication); Log.info ("Authentication passed: {}", username); }                }            } Catch(invalidjwttokenexception invalidjwttokenexception) {response.setcontenttype (Application_json);                Log.error (invalidjwttokenexception.invalid_jwt_token_exception); Resultvo<String> Resultvo =NewResultvo<>(); Resultvo.setsuccess (false);                Resultvo.setmsg (invalidjwttokenexception.invalid_jwt_token_exception); PrintWriter writer=Response.getwriter ();                Writer.write (json.tojsonstring (Resultvo));                Writer.close (); return;    }} chain.dofilter (request, response); }}

Then add in the Config class

@Override protected void throws Exception {http.addfilterbefore (Jwtauthenticationtokenfilter, Usernamepasswordauthenticationfilter. class );}



Spring Security JWT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.