This commit is contained in:
Crystal.Sea
2020-09-09 22:52:03 +08:00
parent 4c86d6860d
commit 8376684a2c
12 changed files with 121 additions and 327 deletions

View File

@@ -46,6 +46,17 @@ public class BasicAuthentication implements Authentication {
grantedAuthority.add(new SimpleGrantedAuthority("ORDINARY_USER"));
}
/**
* BasicAuthentication.
*/
public BasicAuthentication(String username,String password,String authType) {
this.username = username;
this.password = password;
this.authType = authType;
grantedAuthority = new ArrayList<GrantedAuthority>();
grantedAuthority.add(new SimpleGrantedAuthority("ROLE_USER"));
grantedAuthority.add(new SimpleGrantedAuthority("ORDINARY_USER"));
}
@Override
public String getName() {
return "Basic Authentication";

View File

@@ -22,6 +22,7 @@ import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
@@ -103,4 +104,34 @@ public class RealmAuthenticationProvider extends AbstractAuthenticationProvider
return usernamePasswordAuthenticationToken;
}
public Authentication basicAuthenticate(Authentication authentication) {
BasicAuthentication basicAuth = (BasicAuthentication) authentication;
UserInfo loadeduserInfo = loadUserInfo(basicAuth.getUsername(), "");
if (loadeduserInfo != null) {
authenticationRealm.passwordMatches(loadeduserInfo, basicAuth.getPassword());
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(loadeduserInfo);
WebContext.setUserInfo(loadeduserInfo);
authentication.setAuthenticated(true);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
authentication, "PASSWORD", authenticationRealm.grantAuthority(loadeduserInfo));
WebContext.setAuthentication(authenticationToken);
WebContext.setUserInfo(loadeduserInfo);
authenticationRealm.insertLoginHistory(loadeduserInfo, basicAuth.getAuthType(), "", "", "SUCCESS");
return authenticationToken;
}else {
String message = WebContext.getI18nValue("login.error.username");
_logger.debug("login user " + basicAuth.getUsername() + " not in this System ." + message);
throw new BadCredentialsException(WebContext.getI18nValue("login.error.username"));
}
}
}

View File

@@ -86,6 +86,7 @@ public abstract class AbstractAuthenticationRealm {
}
public abstract boolean passwordMatches(UserInfo userInfo, String password);
public static boolean isAuthenticated() {
if (WebContext.getUserInfo() != null) {

View File

@@ -65,4 +65,8 @@ public class DefaultJdbcAuthenticationRealm extends AbstractAuthenticationRealm
}
return passwordMatches;
}
}