未验证 提交 0550f26d 编写于 作者: sinat_25235033's avatar sinat_25235033 提交者: GitHub

implement authorized default in BaseProcessor (#68)

上级 233800e0
...@@ -2,9 +2,12 @@ package com.usthe.sureness.processor; ...@@ -2,9 +2,12 @@ package com.usthe.sureness.processor;
import com.usthe.sureness.processor.exception.SurenessAuthenticationException; import com.usthe.sureness.processor.exception.SurenessAuthenticationException;
import com.usthe.sureness.processor.exception.SurenessAuthorizationException; import com.usthe.sureness.processor.exception.SurenessAuthorizationException;
import com.usthe.sureness.processor.exception.UnauthorizedException;
import com.usthe.sureness.subject.SubjectSum; import com.usthe.sureness.subject.SubjectSum;
import com.usthe.sureness.subject.Subject; import com.usthe.sureness.subject.Subject;
import java.util.List;
/** /**
* abstract processor * abstract processor
* @author tomsun28 * @author tomsun28
...@@ -48,5 +51,13 @@ public abstract class BaseProcessor implements Processor{ ...@@ -48,5 +51,13 @@ public abstract class BaseProcessor implements Processor{
* @param var subject * @param var subject
* @throws SurenessAuthorizationException when authorize error * @throws SurenessAuthorizationException when authorize error
*/ */
public abstract void authorized(Subject var) throws SurenessAuthorizationException; @SuppressWarnings("unchecked")
public void authorized(Subject var) throws SurenessAuthorizationException {
List<String> ownRoles = (List<String>)var.getOwnRoles();
List<String> supportRoles = (List<String>)var.getSupportRoles();
if (supportRoles == null || supportRoles.isEmpty() || supportRoles.stream().anyMatch(ownRoles::contains)) {
return;
}
throw new UnauthorizedException("do not have the role to access resource");
}
} }
...@@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory; ...@@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.List;
/** /**
* process digest auth - DigestSubject * process digest auth - DigestSubject
...@@ -88,17 +87,6 @@ public class DigestProcessor extends BaseProcessor { ...@@ -88,17 +87,6 @@ public class DigestProcessor extends BaseProcessor {
.build(); .build();
} }
@SuppressWarnings("unchecked")
@Override
public void authorized(Subject var) throws SurenessAuthorizationException {
List<String> ownRoles = (List<String>)var.getOwnRoles();
List<String> supportRoles = (List<String>)var.getSupportRoles();
if (supportRoles == null || supportRoles.isEmpty() || supportRoles.stream().anyMatch(ownRoles::contains)) {
return;
}
throw new UnauthorizedException("do not have the role to access resource");
}
private String getAuthenticate(){ private String getAuthenticate(){
String nonce = calcDigest(String.valueOf(System.currentTimeMillis())); String nonce = calcDigest(String.valueOf(System.currentTimeMillis()));
return "Digest " + "realm=" + realm + ",nonce=" + nonce + ",qop=" + qop; return "Digest " + "realm=" + realm + ",nonce=" + nonce + ",qop=" + qop;
......
...@@ -4,8 +4,6 @@ import com.usthe.sureness.processor.BaseProcessor; ...@@ -4,8 +4,6 @@ import com.usthe.sureness.processor.BaseProcessor;
import com.usthe.sureness.processor.exception.ExpiredCredentialsException; import com.usthe.sureness.processor.exception.ExpiredCredentialsException;
import com.usthe.sureness.processor.exception.IncorrectCredentialsException; import com.usthe.sureness.processor.exception.IncorrectCredentialsException;
import com.usthe.sureness.processor.exception.SurenessAuthenticationException; import com.usthe.sureness.processor.exception.SurenessAuthenticationException;
import com.usthe.sureness.processor.exception.SurenessAuthorizationException;
import com.usthe.sureness.processor.exception.UnauthorizedException;
import com.usthe.sureness.subject.PrincipalMap; import com.usthe.sureness.subject.PrincipalMap;
import com.usthe.sureness.subject.Subject; import com.usthe.sureness.subject.Subject;
import com.usthe.sureness.subject.support.JwtSubject; import com.usthe.sureness.subject.support.JwtSubject;
...@@ -80,15 +78,4 @@ public class JwtProcessor extends BaseProcessor { ...@@ -80,15 +78,4 @@ public class JwtProcessor extends BaseProcessor {
return builder.build(); return builder.build();
} }
@SuppressWarnings("unchecked")
@Override
public void authorized(Subject var) throws SurenessAuthorizationException {
List<String> ownRoles = (List<String>)var.getOwnRoles();
List<String> supportRoles = (List<String>)var.getSupportRoles();
if (supportRoles == null || supportRoles.isEmpty() || supportRoles.stream().anyMatch(ownRoles::contains)) {
return;
}
throw new UnauthorizedException("do not have the role to access resource");
}
} }
...@@ -5,8 +5,6 @@ import com.usthe.sureness.processor.exception.DisabledAccountException; ...@@ -5,8 +5,6 @@ import com.usthe.sureness.processor.exception.DisabledAccountException;
import com.usthe.sureness.processor.exception.ExcessiveAttemptsException; import com.usthe.sureness.processor.exception.ExcessiveAttemptsException;
import com.usthe.sureness.processor.exception.IncorrectCredentialsException; import com.usthe.sureness.processor.exception.IncorrectCredentialsException;
import com.usthe.sureness.processor.exception.SurenessAuthenticationException; import com.usthe.sureness.processor.exception.SurenessAuthenticationException;
import com.usthe.sureness.processor.exception.SurenessAuthorizationException;
import com.usthe.sureness.processor.exception.UnauthorizedException;
import com.usthe.sureness.processor.exception.UnknownAccountException; import com.usthe.sureness.processor.exception.UnknownAccountException;
import com.usthe.sureness.provider.SurenessAccount; import com.usthe.sureness.provider.SurenessAccount;
import com.usthe.sureness.provider.SurenessAccountProvider; import com.usthe.sureness.provider.SurenessAccountProvider;
...@@ -16,7 +14,6 @@ import com.usthe.sureness.util.Md5Util; ...@@ -16,7 +14,6 @@ import com.usthe.sureness.util.Md5Util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.List;
/** /**
* the processor support username password - PasswordSubject * the processor support username password - PasswordSubject
...@@ -75,17 +72,6 @@ public class PasswordProcessor extends BaseProcessor { ...@@ -75,17 +72,6 @@ public class PasswordProcessor extends BaseProcessor {
.build(); .build();
} }
@SuppressWarnings("unchecked")
@Override
public void authorized(Subject var) throws SurenessAuthorizationException {
List<String> ownRoles = (List<String>)var.getOwnRoles();
List<String> supportRoles = (List<String>)var.getSupportRoles();
if (supportRoles == null || supportRoles.isEmpty() || supportRoles.stream().anyMatch(ownRoles::contains)) {
return;
}
throw new UnauthorizedException("do not have the role to access resource");
}
public void setAccountProvider(SurenessAccountProvider provider) { public void setAccountProvider(SurenessAccountProvider provider) {
this.accountProvider = provider; this.accountProvider = provider;
} }
......
...@@ -86,7 +86,7 @@ public class CustomTokenProcessor extends BaseProcessor { ...@@ -86,7 +86,7 @@ public class CustomTokenProcessor extends BaseProcessor {
if (supportRoles == null || supportRoles.isEmpty() || supportRoles.stream().anyMatch(ownRoles::contains)) { if (supportRoles == null || supportRoles.isEmpty() || supportRoles.stream().anyMatch(ownRoles::contains)) {
return; return;
} }
throw new UnauthorizedException("do not have the role to access resource"); throw new UnauthorizedException("custom authorized: do not have the role to access resource");
} }
public void setAccountProvider(SurenessAccountProvider accountProvider) { public void setAccountProvider(SurenessAccountProvider accountProvider) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册