forked from hengyunabc/xdiamond
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b90e54
commit 81ae775
Showing
8 changed files
with
137 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...rver/src/main/java/io/github/xdiamond/web/shiro/CustomPermissionsAuthorizationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.github.xdiamond.web.shiro; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.shiro.subject.Subject; | ||
import org.apache.shiro.util.StringUtils; | ||
import org.apache.shiro.web.filter.authz.AuthorizationFilter; | ||
import org.apache.shiro.web.util.WebUtils; | ||
|
||
/** | ||
* ajax request/accept json return JSON, not ajax request will redirect to unauthorizedUrl. | ||
* | ||
* @author hengyunabc | ||
* | ||
*/ | ||
public class CustomPermissionsAuthorizationFilter extends AuthorizationFilter { | ||
|
||
String unauthorizedJSONString = | ||
"{\"success\":false,\"error\":{\"message\":\"unauthorized! need login\"}}"; | ||
|
||
String forbiddenJSONString = | ||
"{\"success\":false,\"error\":{\"message\":\"forbidden! no permission.\"}}"; | ||
|
||
@Override | ||
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) | ||
throws IOException { | ||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
|
||
Subject subject = getSubject(request, response); | ||
|
||
if (subject.getPrincipal() == null) { | ||
if (AjaxUtils.isAjaxRequest(httpRequest) || AjaxUtils.isAcceptJSON(httpRequest)) { | ||
WebUtil.wrietJSONResponse(httpResponse, unauthorizedJSONString, | ||
HttpServletResponse.SC_UNAUTHORIZED); | ||
} else { | ||
saveRequestAndRedirectToLogin(request, response); | ||
} | ||
} else { | ||
if (AjaxUtils.isAjaxRequest(httpRequest) || AjaxUtils.isAcceptJSON(httpRequest)) { | ||
WebUtil.wrietJSONResponse(httpResponse, forbiddenJSONString, | ||
HttpServletResponse.SC_FORBIDDEN); | ||
} else { | ||
String unauthorizedUrl = getUnauthorizedUrl(); | ||
if (StringUtils.hasText(unauthorizedUrl)) { | ||
WebUtils.issueRedirect(request, response, unauthorizedUrl); | ||
} else { | ||
WebUtils.toHttp(response).sendError(401); | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isAccessAllowed(ServletRequest request, ServletResponse response, | ||
Object mappedValue) throws IOException { | ||
|
||
Subject subject = getSubject(request, response); | ||
String[] permissionsArray = (String[]) mappedValue; | ||
|
||
if (permissionsArray == null || permissionsArray.length == 0) { | ||
// no roles specified, so nothing to check - allow access. | ||
return true; | ||
} | ||
return subject.isPermittedAll(permissionsArray); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters