@ -53,7 +53,9 @@ import java.util.Map;
@@ -53,7 +53,9 @@ import java.util.Map;
* Such that "id" is an identifier used to look up which { @link PasswordEncoder } should be
* used and "encodedPassword" is the original encoded password for the selected
* { @link PasswordEncoder } . The "id" must be at the beginning of the password , start with
* "{" and end with "}" . If the "id" cannot be found , the "id" will be null .
* "{" ( id prefix ) and end with "}" ( id suffix ) . Both id prefix and id suffix can be
* customized via { @link # DelegatingPasswordEncoder ( String , Map , String , String ) } . If the
* "id" cannot be found , the "id" will be null .
*
* For example , the following might be a list of passwords encoded using different "id" .
* All of the original passwords are "password" .
@ -122,13 +124,13 @@ import java.util.Map;
@@ -122,13 +124,13 @@ import java.util.Map;
* /
public class DelegatingPasswordEncoder implements PasswordEncoder {
private static final String DEFAULT_PREFIX = "{" ;
private static final String DEFAULT_ID_ PREFIX = "{" ;
private static final String DEFAULT_SUFFIX = "}" ;
private static final String DEFAULT_ID_ SUFFIX = "}" ;
private final String p refix;
private final String idP refix;
private final String s uffix;
private final String idS uffix;
private final String idForEncode ;
@ -147,7 +149,7 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@@ -147,7 +149,7 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
* { @link # matches ( CharSequence , String ) }
* /
public DelegatingPasswordEncoder ( String idForEncode , Map < String , PasswordEncoder > idToPasswordEncoder ) {
this ( idForEncode , idToPasswordEncoder , DEFAULT_PREFIX , DEFAULT_SUFFIX ) ;
this ( idForEncode , idToPasswordEncoder , DEFAULT_ID_ PREFIX , DEFAULT_ID _SUFFIX ) ;
}
/ * *
@ -156,19 +158,19 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@@ -156,19 +158,19 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
* used for { @link # encode ( CharSequence ) }
* @param idToPasswordEncoder a Map of id to { @link PasswordEncoder } used to determine
* which { @link PasswordEncoder } should be used for
* @param p refix the prefix that denotes the start of an { @code idForEncode }
* @param s uffix the suffix that denotes the end of an { @code idForEncode }
* @param idP refix the prefix that denotes the start of the id in the encoded results
* @param idS uffix the suffix that denotes the end of an id in the encoded results
* { @link # matches ( CharSequence , String ) }
* /
public DelegatingPasswordEncoder ( String idForEncode , Map < String , PasswordEncoder > idToPasswordEncoder ,
String prefix , String s uffix) {
String idPrefix , String idS uffix) {
if ( idForEncode = = null ) {
throw new IllegalArgumentException ( "idForEncode cannot be null" ) ;
}
if ( p refix = = null ) {
if ( idP refix = = null ) {
throw new IllegalArgumentException ( "prefix cannot be null" ) ;
}
if ( s uffix = = null | | s uffix. isEmpty ( ) ) {
if ( idS uffix = = null | | idS uffix. isEmpty ( ) ) {
throw new IllegalArgumentException ( "suffix cannot be empty" ) ;
}
@ -180,18 +182,18 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@@ -180,18 +182,18 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
if ( id = = null ) {
continue ;
}
if ( ! p refix. isEmpty ( ) & & id . contains ( p refix) ) {
throw new IllegalArgumentException ( "id " + id + " cannot contain " + p refix) ;
if ( ! idP refix. isEmpty ( ) & & id . contains ( idP refix) ) {
throw new IllegalArgumentException ( "id " + id + " cannot contain " + idP refix) ;
}
if ( id . contains ( s uffix) ) {
throw new IllegalArgumentException ( "id " + id + " cannot contain " + s uffix) ;
if ( id . contains ( idS uffix) ) {
throw new IllegalArgumentException ( "id " + id + " cannot contain " + idS uffix) ;
}
}
this . idForEncode = idForEncode ;
this . passwordEncoderForEncode = idToPasswordEncoder . get ( idForEncode ) ;
this . idToPasswordEncoder = new HashMap < > ( idToPasswordEncoder ) ;
this . prefix = p refix;
this . suffix = s uffix;
this . idPrefix = idP refix;
this . idSuffix = idS uffix;
}
/ * *
@ -217,7 +219,7 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@@ -217,7 +219,7 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@Override
public String encode ( CharSequence rawPassword ) {
return this . p refix + this . idForEncode + this . s uffix + this . passwordEncoderForEncode . encode ( rawPassword ) ;
return this . idP refix + this . idForEncode + this . idS uffix + this . passwordEncoderForEncode . encode ( rawPassword ) ;
}
@Override
@ -238,15 +240,15 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@@ -238,15 +240,15 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
if ( prefixEncodedPassword = = null ) {
return null ;
}
int start = prefixEncodedPassword . indexOf ( this . p refix) ;
int start = prefixEncodedPassword . indexOf ( this . idP refix) ;
if ( start ! = 0 ) {
return null ;
}
int end = prefixEncodedPassword . indexOf ( this . s uffix, start ) ;
int end = prefixEncodedPassword . indexOf ( this . idS uffix, start ) ;
if ( end < 0 ) {
return null ;
}
return prefixEncodedPassword . substring ( start + this . p refix. length ( ) , end ) ;
return prefixEncodedPassword . substring ( start + this . idP refix. length ( ) , end ) ;
}
@Override
@ -262,8 +264,8 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
@@ -262,8 +264,8 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
}
private String extractEncodedPassword ( String prefixEncodedPassword ) {
int start = prefixEncodedPassword . indexOf ( this . s uffix) ;
return prefixEncodedPassword . substring ( start + this . s uffix. length ( ) ) ;
int start = prefixEncodedPassword . indexOf ( this . idS uffix) ;
return prefixEncodedPassword . substring ( start + this . idS uffix. length ( ) ) ;
}
/ * *