Spring boot integrates with spring Security's encountered pits in the upgrade of spring release

Source: Internet
Author: User

In the version of Spring security3.x

Hasanyrole This method does not add a prefix to the identity that we need to authenticate role_, in the 3.x version hasrole the source code is as follows

     Public Final Boolean hasanyrole (String ... roles) {        Set<String> roleset = getauthorityset ();          for (String role:roles) {            if  (roleset.contains (role)) {                return true ;            }        }         return false ;    }

And the 4.x version will be based on my specific situation to see if you want to prefix, the code is as follows

     Public Final Booleanhasanyrole (String ... roles) {returnHasanyauthorityname (defaultroleprefix, roles); }    Private Booleanhasanyauthorityname (string prefix, string ... roles) {Set<String> Roleset =Getauthorityset ();  for(String role:roles) {string Defaultedrole=getrolewithdefaultprefix (prefix, role); if(Roleset.contains (defaultedrole)) {return true; }        }        return false; }
    Private Staticstring Getrolewithdefaultprefix (String defaultroleprefix, String role) {if(Role = =NULL) {            returnrole; }        if(Defaultroleprefix = =NULL|| Defaultroleprefix.length () = = 0) {            returnrole; }        if(Role.startswith (Defaultroleprefix)) {returnrole; }        returnDefaultroleprefix +role; }

Here the pit to be careful, if you add the Role_ prefix, then you log on the role of the front also need to add role_, otherwise it will fail authentication, then the 403 page. (Ps:hasrole This method also has this version problem, specifically also and hasanyrole is not much.) )

Spring boot integrates with spring Security's encountered pits in the upgrade of spring release

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.