本文整理汇总了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;
}