本文整理匯總了Java中org.apache.shiro.web.util.WebUtils類的典型用法代碼示例。如果您正苦於以下問題:Java WebUtils類的具體用法?Java WebUtils怎麽用?Java WebUtils使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
WebUtils類屬於org.apache.shiro.web.util包,在下文中一共展示了WebUtils類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onPreHandle
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
if (this.isAccessAllowed(request, response, mappedValue) && this.isLoginRequest(request, response)) {
if (((HttpServletRequest)request).getRequestURL().toString().endsWith(".json")){
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("{\"code\":200,\"info\":\"already logined\"}");
out.flush();
out.close();
}else {
WebUtils.issueRedirect(request,response,this.getSuccessUrl());
}
return false;
}
return super.onPreHandle(request, response, mappedValue);
}
示例2: issueSuccessRedirect
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
protected void issueSuccessRedirect(ServletRequest request, ServletResponse response)
throws Exception {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String successUrl = req.getParameter(RETURN_URL);
if (StringUtils.isBlank(successUrl)) {
if (req.getRequestURI().startsWith(
req.getContextPath() + getAdminPrefix())) {
// 後台直接返回首頁
successUrl = getAdminIndex();
// 清除SavedRequest
WebUtils.getAndClearSavedRequest(request);
WebUtils.issueRedirect(request, response, successUrl, null,true);
return;
} else {
successUrl = getSuccessUrl();
}
}
WebUtils.redirectToSavedRequest(req, res, successUrl);
}
示例3: getSessionId
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
// 如果參數中包含“__sid”參數,則使用此sid會話。 例如:http://localhost/project?__sid=xxx&__cookie=true
String sid = request.getParameter("__sid");
if (StringUtils.isNotBlank(sid)) {
// 是否將sid保存到cookie,瀏覽器模式下使用此參數。
if (WebUtils.isTrue(request, "__cookie")) {
HttpServletRequest rq = (HttpServletRequest) request;
HttpServletResponse rs = (HttpServletResponse) response;
Cookie template = getSessionIdCookie();
Cookie cookie = new SimpleCookie(template);
cookie.setValue(sid);
cookie.saveTo(rq, rs);
}
// 設置當前session狀態
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, ShiroHttpServletRequest.URL_SESSION_ID_SOURCE); // session來源與url
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sid);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
return sid;
} else {
return super.getSessionId(request, response);
}
}
示例4: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if(isAsynRequest(req, res)){
WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
Result<Object> result = new Result<Object>();
result.setCode("401");
result.setSuccess(false);
result.setMessage(Messages.getMessage(getDefaultNoPermissionMessageI18nCode(), null, getDefaultNoPermissionMessage()));
res.setCharacterEncoding("UTF-8");
res.setContentType("application/json;charset=UTF-8");
PrintWriter out = res.getWriter();
out.println(JsonUtils.object2Json(result));
out.flush();
out.close();
return false;
}else{
return super.onAccessDenied(request, response);
}
}
示例5: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if(isAsynRequest(req, res)){
WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
Result<Object> result = new Result<Object>();
result.setCode("401");
result.setSuccess(false);
result.setMessage(Messages.getMessage(getDefaultNoPermissionMessageI18nCode(), null, getDefaultNoPermissionMessage()));
res.setCharacterEncoding("UTF-8");
res.setContentType("application/json;charset=UTF-8");
PrintWriter out = res.getWriter();
out.println(JsonUtils.object2Json(result));
out.flush();
out.close();
return false;
}else{
return super.onAccessDenied(request, response);
}
}
示例6: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
StringBuffer sso_server_url = new StringBuffer(PropertiesFileUtil.getInstance("zheng-upms-client").get("zheng.upms.sso.server.url"));
// server需要登錄
String upmsType = PropertiesFileUtil.getInstance("zheng-upms-client").get("zheng.upms.type");
if ("server".equals(upmsType)) {
WebUtils.toHttp(response).sendRedirect(sso_server_url.append("/sso/login").toString());
return false;
}
sso_server_url.append("/sso/index").append("?").append("appid").append("=").append(PropertiesFileUtil.getInstance("zheng-upms-client").get("zheng.upms.appID"));
// 回跳地址
HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
StringBuffer backurl = httpServletRequest.getRequestURL();
String queryString = httpServletRequest.getQueryString();
if (StringUtils.isNotBlank(queryString)) {
backurl.append("?").append(queryString);
}
sso_server_url.append("&").append("backurl").append("=").append(URLEncoder.encode(backurl.toString(), "utf-8"));
WebUtils.toHttp(response).sendRedirect(sso_server_url.toString());
return false;
}
示例7: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
String error = request.getParameter("error");
String errorDescription = request.getParameter("error_description");
if(!StringUtils.isEmpty(error)) {//如果服務端返回了錯誤
WebUtils.issueRedirect(request, response, failureUrl + "?error=" + error + "error_description=" + errorDescription);
return false;
}
Subject subject = getSubject(request, response);
if(!subject.isAuthenticated()) {
if(StringUtils.isEmpty(request.getParameter(authcCodeParam))) {
//如果用戶沒有身份驗證,且沒有auth code,則重定向到服務端授權
saveRequestAndRedirectToLogin(request, response);
return false;
}
}
return executeLogin(request, response);
}
示例8: isAccessAllowed
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
throws Exception {
// 1、設置驗證碼是否開啟屬性,頁麵可以根據該屬性來決定是否顯示驗證碼
request.setAttribute("captchaEbabled", captchaEbabled);
HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
// 2、判斷驗證碼是否禁用 或不是表單提交(允許訪問)
if (captchaEbabled == false || !"post".equalsIgnoreCase(httpServletRequest.getMethod())) {
return true;
}
// 3、此時是表單提交,驗證驗證碼是否正確
// 獲取頁麵提交的驗證碼
String submitCaptcha = httpServletRequest.getParameter(captchaParam);
// 獲取session中的驗證碼
String captcha = (String) httpServletRequest.getSession().getAttribute("rcCaptcha");
if (submitCaptcha.equals(captcha)) {
return true;
}
return false;
}
示例9: doPost
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
public void doPost(MCRServletJob job) throws Exception {
Element output = new Element("userLogging");
String username = getParameter(job, "username");
String password = getParameter(job, "password");
boolean rememberMe = "true".equals(getParameter(job, "rememberMe"));
boolean b = false;
if (username == null)
output.addContent((new Element("message")).addContent("login.message.noUserGiven"));
else if (password == null)
output.addContent((new Element("message")).addContent("login.message.noPasswordGiven"));
else {
b = tryLogin(username, password, rememberMe);
if (b) {
SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(job.getRequest());
if (savedRequest != null)
job.getResponse().sendRedirect(savedRequest.getRequestUrl());
else
job.getResponse().sendRedirect("analysis/start");
} else
output.addContent((new Element("message")).addContent("login.message.loginFailed"));
}
sendOutput(job, output);
}
示例10: onPreHandle
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
Subject subject = SecurityUtils.getSubject();
if (!subject.isAuthenticated()) {
HttpServletRequest httpRequest = WebUtils.toHttp(request);
String authzHeader = httpRequest.getHeader(AUTHORIZATION_HEADER);
if (authzHeader != null) {
if (authzHeader.toLowerCase(Locale.ENGLISH).startsWith("basic")
|| authzHeader.toLowerCase(Locale.ENGLISH).startsWith("token")) {
String authToken = StringUtils.substringAfter(authzHeader, " ");
String decoded = Base64.decodeToString(authToken);
String userName = StringUtils.substringBefore(decoded, ":").trim();
String password = StringUtils.substringAfter(decoded, ":").trim();
if (userName.length() != 0 && password.length() != 0) {
UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
subject.login(token);
}
}
}
}
return true;
}
示例11: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request,
ServletResponse response) throws Exception {
Subject subject = getSubject(request, response);
if (null == subject.getPrincipal()) {//表示沒有登錄,重定向到登錄頁麵
saveRequest(request);
WebUtils.issueRedirect(request, response, ShiroUtils.LOGIN_URL);
} else {
if(ShiroUtils.isAjax(request)){
Map<String, Object> result = new HashMap<String, Object>();
result.put("status", "401");
result.put("message", "sorry,您沒有權限");
result.put("url", ShiroUtils.UNAUTHORIZED);
ShiroUtils.writeJson(response, result);
}else
{
if (StringUtils.hasText(ShiroUtils.UNAUTHORIZED)) {//如果有未授權頁麵跳轉過去
WebUtils.issueRedirect(request, response, ShiroUtils.UNAUTHORIZED);
} else {//否則返回401未授權狀態碼
WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
}
return Boolean.FALSE;
}
示例12: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request,
ServletResponse response) throws Exception {
//先退出
Subject subject = getSubject(request, response);
subject.logout();
/**
* shiro保存上次請求地址:
* WebUtils.getSavedRequest(request);
* 然後在需要使用的地方:String url = WebUtils.getSavedRequest(request).getRequestUrl();
*/
//判斷是不是Ajax請求
if (ShiroUtils.isAjax(request) ) {
Map<String, Object> result = new HashMap<String, Object>();
logger.debug("當前用戶已經被踢出,並且是Ajax請求!");
result.put("status", "403");
result.put("message", "您已經被踢出,請重新登錄!");
result.put("url", ShiroUtils.LOGIN_URL);
ShiroUtils.writeJson(response, result);
}else
{
WebUtils.getSavedRequest(request);
//再重定向
WebUtils.issueRedirect(request, response, ShiroUtils.LOGIN_URL);
}
return false;
}
示例13: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request,
ServletResponse response) throws Exception {
Subject subject = getSubject(request, response);
if (subject.getPrincipal() == null) {//表示沒有登錄,重定向到登錄頁麵
saveRequest(request);
WebUtils.issueRedirect(request, response, ShiroUtils.LOGIN_URL);
} else {
if(ShiroUtils.isAjax(request)){
Map<String, Object> result = new HashMap<String, Object>();
result.put("status", "401");
result.put("message", "sorry,您沒有權限");
result.put("url", ShiroUtils.UNAUTHORIZED);
ShiroUtils.writeJson(response, result);
}else
{
if (StringUtils.hasText(ShiroUtils.UNAUTHORIZED)) {//如果有未授權頁麵跳轉過去
WebUtils.issueRedirect(request, response, ShiroUtils.UNAUTHORIZED);
} else {//否則返回401未授權狀態碼
WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
}
return false;
}
示例14: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request,
ServletResponse response) throws Exception {
if(ShiroUtils.isAjax(request)){
Map<String, Object> result = new HashMap<String, Object>();
result.put("status", "401");
result.put("message", "非法操作");
result.put("url", ShiroUtils.INDEX_URL);
ShiroUtils.writeJson(response, result);
}else
{
if (StringUtils.hasText(ShiroUtils.INDEX_URL)) {//如果有未授權頁麵跳轉過去
WebUtils.issueRedirect(request, response, ShiroUtils.INDEX_URL);
} else {//否則返回401未授權狀態碼
WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
return Boolean.FALSE;
}
示例15: onAccessDenied
import org.apache.shiro.web.util.WebUtils; //導入依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
StringBuffer sso_server_url = new StringBuffer(PropertiesFileUtil.getInstance("lambo-upms-client").get("lambo.upms.sso.server.url"));
// server需要登錄
String upmsType = PropertiesFileUtil.getInstance("lambo-upms-client").get("lambo.upms.type");
if ("server".equals(upmsType)) {
WebUtils.toHttp(response).sendRedirect(sso_server_url.append("/sso/login").toString());
return false;
}
sso_server_url.append("/sso/index").append("?").append("appid").append("=").append(PropertiesFileUtil.getInstance("lambo-upms-client").get("lambo.upms.appID"));
// 回跳地址
HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
StringBuffer backurl = httpServletRequest.getRequestURL();
String queryString = httpServletRequest.getQueryString();
if (StringUtils.isNotBlank(queryString)) {
backurl.append("?").append(queryString);
}
sso_server_url.append("&").append("backurl").append("=").append(URLEncoder.encode(backurl.toString(), "utf-8"));
WebUtils.toHttp(response).sendRedirect(sso_server_url.toString());
return false;
}