本文整理汇总了Java中org.apache.shiro.util.StringUtils类的典型用法代码示例。如果您正苦于以下问题:Java StringUtils类的具体用法?Java StringUtils怎么用?Java StringUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StringUtils类属于org.apache.shiro.util包,在下文中一共展示了StringUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onAccessDenied
import org.apache.shiro.util.StringUtils; //导入依赖的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;
}
示例2: onAccessDenied
import org.apache.shiro.util.StringUtils; //导入依赖的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;
}
示例3: onAccessDenied
import org.apache.shiro.util.StringUtils; //导入依赖的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;
}
示例4: buildHeaderValue
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
protected String buildHeaderValue(String name, String value, String comment,
String domain, String path, int maxAge, int version,
boolean secure, boolean httpOnly) {
if (!StringUtils.hasText(name)) {
throw new IllegalStateException("Cookie name cannot be null/empty.");
}
StringBuilder sb = new StringBuilder(name).append(NAME_VALUE_DELIMITER);
if (StringUtils.hasText(value)) {
sb.append(value);
}
appendComment(sb, comment);
appendDomain(sb, domain);
appendPath(sb, path);
appendExpires(sb, maxAge);
appendVersion(sb, version);
appendSecure(sb, secure);
appendHttpOnly(sb, httpOnly);
return sb.toString();
}
示例5: readValue
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
public String readValue(HttpServletRequest request, HttpServletResponse ignored) {
String name = getName();
String value = null;
javax.servlet.http.Cookie cookie = getCookie(request, name);
if (cookie != null) {
// Validate that the cookie is used at the correct place.
String path = StringUtils.clean(getPath());
if (path != null && !pathMatches(path, request.getRequestURI())) {
log.warn("Found '{}' cookie at path '{}', but should be only used for '{}'", new Object[] { name, request.getRequestURI(), path});
} else {
value = cookie.getValue();
log.debug("Found '{}' cookie value [{}]", name, value);
}
} else {
log.trace("No '{}' cookie value", name);
}
return value;
}
示例6: addToChain
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
public void addToChain(String chainName, String filterName, String chainSpecificFilterConfig) {
if (!StringUtils.hasText(chainName)) {
throw new IllegalArgumentException("chainName cannot be null or empty.");
}
Filter filter = getFilter(filterName);
if (filter == null) {
throw new IllegalArgumentException("There is no filter with name '" + filterName +
"' to apply to chain [" + chainName + "] in the pool of available Filters. Ensure a " +
"filter with that name/path has first been registered with the addFilter method(s).");
}
applyChainConfig(chainName, filter, chainSpecificFilterConfig);
NamedFilterList chain = ensureChain(chainName);
chain.add(filter);
}
示例7: applyChainConfig
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
protected void applyChainConfig(String chainName, Filter filter, String chainSpecificFilterConfig) {
if (log.isDebugEnabled()) {
log.debug("Attempting to apply path [" + chainName + "] to filter [" + filter + "] " +
"with config [" + chainSpecificFilterConfig + "]");
}
if (filter instanceof PathConfigProcessor) {
((PathConfigProcessor) filter).processPathConfig(chainName, chainSpecificFilterConfig);
} else {
if (StringUtils.hasText(chainSpecificFilterConfig)) {
//they specified a filter configuration, but the Filter doesn't implement PathConfigProcessor
//this is an erroneous config:
String msg = "chainSpecificFilterConfig was specified, but the underlying " +
"Filter instance is not an 'instanceof' " +
PathConfigProcessor.class.getName() + ". This is required if the filter is to accept " +
"chain-specific configuration.";
throw new ConfigurationException(msg);
}
}
}
示例8: setAuthorizedHosts
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
public void setAuthorizedHosts(String authorizedHosts) {
if (!StringUtils.hasText(authorizedHosts)) {
throw new IllegalArgumentException("authorizedHosts argument cannot be null or empty.");
}
String[] hosts = StringUtils.tokenizeToStringArray(authorizedHosts, ", \t");
for (String host : hosts) {
//replace any periods with \\. to ensure the regex works:
String periodsReplaced = host.replace(".", "\\.");
//check for IPv4:
String wildcardsReplaced = periodsReplaced.replace("*", IPV4_QUAD_REGEX);
if (IPV4_PATTERN.matcher(wildcardsReplaced).matches()) {
authorizedIps.put(host, wildcardsReplaced);
} else {
}
}
}
示例9: isIpv4Candidate
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
protected boolean isIpv4Candidate(String host) {
String[] quads = StringUtils.tokenizeToStringArray(host, ".");
if (quads == null || quads.length != 4) {
return false;
}
for (String quad : quads) {
if (!quad.equals("*")) {
try {
Integer.parseInt(quad);
} catch (NumberFormatException nfe) {
return false;
}
}
}
return true;
}
示例10: encodeParts
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
private void encodeParts(String domain, String actions, String targets) {
if (!StringUtils.hasText(domain)) {
throw new IllegalArgumentException("domain argument cannot be null or empty.");
}
StringBuilder sb = new StringBuilder(domain);
if (!StringUtils.hasText(actions)) {
if (StringUtils.hasText(targets)) {
sb.append(PART_DIVIDER_TOKEN).append(WILDCARD_TOKEN);
}
} else {
sb.append(PART_DIVIDER_TOKEN).append(actions);
}
if (StringUtils.hasText(targets)) {
sb.append(PART_DIVIDER_TOKEN).append(targets);
}
setParts(sb.toString());
}
示例11: validateAuthenticationInfo
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
/**
* Validates the configuration in the JNDI <code>environment</code> settings and throws an exception if a problem
* exists.
* <p/>
* This implementation will throw a {@link AuthenticationException} if the authentication mechanism is set to
* 'simple', the principal is non-empty, and the credentials are empty (as per
* <a href="http://tools.ietf.org/html/rfc4513#section-5.1.2">rfc4513 section-5.1.2</a>).
*
* @param environment the JNDI environment settings to be validated
* @throws AuthenticationException if a configuration problem is detected
*/
protected void validateAuthenticationInfo(Hashtable<String, Object> environment)
throws AuthenticationException
{
// validate when using Simple auth both principal and credentials are set
if(SIMPLE_AUTHENTICATION_MECHANISM_NAME.equals(environment.get(Context.SECURITY_AUTHENTICATION))) {
// only validate credentials if we have a non-empty principal
if( environment.get(Context.SECURITY_PRINCIPAL) != null &&
StringUtils.hasText( String.valueOf( environment.get(Context.SECURITY_PRINCIPAL) ))) {
Object credentials = environment.get(Context.SECURITY_CREDENTIALS);
// from the FAQ, we need to check for empty credentials:
// http://docs.oracle.com/javase/tutorial/jndi/ldap/faq.html
if( credentials == null ||
(credentials instanceof byte[] && ((byte[])credentials).length <= 0) || // empty byte[]
(credentials instanceof char[] && ((char[])credentials).length <= 0) || // empty char[]
(String.class.isInstance(credentials) && !StringUtils.hasText(String.valueOf(credentials)))) {
throw new javax.naming.AuthenticationException("LDAP Simple authentication requires both a "
+ "principal and credentials.");
}
}
}
}
示例12: getUserDn
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
/**
* Returns the LDAP User Distinguished Name (DN) to use when acquiring an
* {@link javax.naming.ldap.LdapContext LdapContext} from the {@link LdapContextFactory}.
* <p/>
* If the the {@link #getUserDnTemplate() userDnTemplate} property has been set, this implementation will construct
* the User DN by substituting the specified {@code principal} into the configured template. If the
* {@link #getUserDnTemplate() userDnTemplate} has not been set, the method argument will be returned directly
* (indicating that the submitted authentication token principal <em>is</em> the User DN).
*
* @param principal the principal to substitute into the configured {@link #getUserDnTemplate() userDnTemplate}.
* @return the constructed User DN to use at runtime when acquiring an {@link javax.naming.ldap.LdapContext}.
* @throws IllegalArgumentException if the method argument is null or empty
* @throws IllegalStateException if the {@link #getUserDnTemplate userDnTemplate} has not been set.
* @see LdapContextFactory#getLdapContext(Object, Object)
*/
protected String getUserDn(String principal) throws IllegalArgumentException, IllegalStateException {
if (!StringUtils.hasText(principal)) {
throw new IllegalArgumentException("User principal cannot be null or empty for User DN construction.");
}
String prefix = getUserDnPrefix();
String suffix = getUserDnSuffix();
if (prefix == null && suffix == null) {
log.debug("userDnTemplate property has not been configured, indicating the submitted " +
"AuthenticationToken's principal is the same as the User DN. Returning the method argument " +
"as is.");
return principal;
}
int prefixLength = prefix != null ? prefix.length() : 0;
int suffixLength = suffix != null ? suffix.length() : 0;
StringBuilder sb = new StringBuilder(prefixLength + principal.length() + suffixLength);
if (prefixLength > 0) {
sb.append(prefix);
}
sb.append(principal);
if (suffixLength > 0) {
sb.append(suffix);
}
return sb.toString();
}
示例13: validateAuthenticationInfo
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
/**
* Validates the configuration in the JNDI <code>environment</code> settings and throws an exception if a problem
* exists.
* <p/>
* This implementation will throw a {@link AuthenticationException} if the authentication mechanism is set to
* 'simple', the principal is non-empty, and the credentials are empty (as per
* <a href="http://tools.ietf.org/html/rfc4513#section-5.1.2">rfc4513 section-5.1.2</a>).
*
* @param environment the JNDI environment settings to be validated
* @throws AuthenticationException if a configuration problem is detected
*/
private void validateAuthenticationInfo(Hashtable<String, Object> environment)
throws AuthenticationException
{
// validate when using Simple auth both principal and credentials are set
if(SIMPLE_AUTHENTICATION_MECHANISM_NAME.equals(environment.get(Context.SECURITY_AUTHENTICATION))) {
// only validate credentials if we have a non-empty principal
if( environment.get(Context.SECURITY_PRINCIPAL) != null &&
StringUtils.hasText( String.valueOf( environment.get(Context.SECURITY_PRINCIPAL) ))) {
Object credentials = environment.get(Context.SECURITY_CREDENTIALS);
// from the FAQ, we need to check for empty credentials:
// http://docs.oracle.com/javase/tutorial/jndi/ldap/faq.html
if( credentials == null ||
(credentials instanceof byte[] && ((byte[])credentials).length <= 0) || // empty byte[]
(credentials instanceof char[] && ((char[])credentials).length <= 0) || // empty char[]
(String.class.isInstance(credentials) && !StringUtils.hasText(String.valueOf(credentials)))) {
throw new javax.naming.AuthenticationException("LDAP Simple authentication requires both a "
+ "principal and credentials.");
}
}
}
}
示例14: getCache
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
/**
* Returns the cache with the specified {@code name}. If the cache instance does not yet exist, it will be lazily
* created, retained for further access, and then returned.
*
* @param name the name of the cache to acquire.
* @return the cache with the specified {@code name}.
* @throws IllegalArgumentException if the {@code name} argument is {@code null} or does not contain text.
* @throws CacheException if there is a problem lazily creating a {@code Cache} instance.
*/
public <K, V> Cache<K, V> getCache(String name) throws IllegalArgumentException, CacheException {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Cache name cannot be null or empty.");
}
Cache cache;
cache = caches.get(name);
if (cache == null) {
cache = createCache(name);
Cache existing = caches.putIfAbsent(name, cache);
if (existing != null) {
cache = existing;
}
}
//noinspection unchecked
return cache;
}
示例15: Section
import org.apache.shiro.util.StringUtils; //导入依赖的package包/类
private Section(String name, String sectionContent) {
if (name == null) {
throw new NullPointerException("name");
}
this.name = name;
Map<String,String> props;
if (StringUtils.hasText(sectionContent) ) {
props = toMapProps(sectionContent);
} else {
props = new LinkedHashMap<String,String>();
}
if ( props != null ) {
this.props = props;
} else {
this.props = new LinkedHashMap<String,String>();
}
}