| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ShiroSecurityContext.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Support :: JAX-RS</a> > <a href="index.source.html" class="el_package">org.apache.shiro.web.jaxrs</a> > <span class="el_source">ShiroSecurityContext.java</span></div><h1>ShiroSecurityContext.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.web.jaxrs; |
| |
| import org.apache.shiro.SecurityUtils; |
| import org.apache.shiro.subject.PrincipalCollection; |
| import org.apache.shiro.subject.Subject; |
| |
| import javax.ws.rs.container.ContainerRequestContext; |
| import javax.ws.rs.core.SecurityContext; |
| import java.security.Principal; |
| |
| /** |
| * A Shiro based {@link SecurityContext} that exposes the current Shiro {@link Subject} as a {@link Principal}. |
| * The {@link #isUserInRole(String)} method returns the result of {@link Subject#hasRole(String)}. |
| * |
| * @since 1.4 |
| */ |
| public class ShiroSecurityContext implements SecurityContext { |
| |
| final private ContainerRequestContext containerRequestContext; |
| |
| <span class="fc" id="L39"> public ShiroSecurityContext(ContainerRequestContext containerRequestContext) {</span> |
| <span class="fc" id="L40"> this.containerRequestContext = containerRequestContext;</span> |
| <span class="fc" id="L41"> }</span> |
| |
| @Override |
| public Principal getUserPrincipal() { |
| |
| Principal result; |
| |
| <span class="fc" id="L48"> Subject subject = getSubject();</span> |
| <span class="fc" id="L49"> PrincipalCollection shiroPrincipals = subject.getPrincipals();</span> |
| <span class="fc bfc" id="L50" title="All 2 branches covered."> if (shiroPrincipals != null) {</span> |
| <span class="fc" id="L51"> result = shiroPrincipals.oneByType(Principal.class);</span> |
| |
| <span class="fc bfc" id="L53" title="All 2 branches covered."> if (result == null) {</span> |
| <span class="fc" id="L54"> result = new ObjectPrincipal(shiroPrincipals.getPrimaryPrincipal());</span> |
| } |
| } |
| else { |
| <span class="fc" id="L58"> result = containerRequestContext.getSecurityContext().getUserPrincipal();</span> |
| } |
| |
| <span class="fc" id="L61"> return result;</span> |
| } |
| |
| @Override |
| public boolean isUserInRole(String role) { |
| <span class="fc" id="L66"> return getSubject().hasRole(role);</span> |
| } |
| |
| @Override |
| public boolean isSecure() { |
| <span class="fc" id="L71"> return containerRequestContext.getSecurityContext().isSecure();</span> |
| } |
| |
| @Override |
| public String getAuthenticationScheme() { |
| <span class="fc" id="L76"> return containerRequestContext.getSecurityContext().getAuthenticationScheme();</span> |
| } |
| |
| private Subject getSubject() { |
| <span class="fc" id="L80"> return SecurityUtils.getSubject();</span> |
| } |
| |
| |
| /** |
| * Java Principal wrapper around any Shiro Principal object.s |
| */ |
| private class ObjectPrincipal implements Principal { |
| <span class="fc" id="L88"> private Object object = null;</span> |
| |
| <span class="fc" id="L90"> public ObjectPrincipal(Object object) {</span> |
| <span class="fc" id="L91"> this.object = object;</span> |
| <span class="fc" id="L92"> }</span> |
| |
| public Object getObject() { |
| <span class="fc" id="L95"> return object;</span> |
| } |
| |
| public String getName() { |
| <span class="fc" id="L99"> return getObject().toString();</span> |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| <span class="pc bpc" id="L104" title="1 of 2 branches missed."> if (this == o) return true;</span> |
| <span class="pc bpc" id="L105" title="2 of 4 branches missed."> if (o == null || getClass() != o.getClass()) return false;</span> |
| |
| <span class="fc" id="L107"> ObjectPrincipal that = (ObjectPrincipal) o;</span> |
| |
| <span class="fc" id="L109"> return object.equals(that.object);</span> |
| |
| } |
| |
| public int hashCode() { |
| <span class="nc" id="L114"> return object.hashCode();</span> |
| } |
| |
| public String toString() { |
| <span class="nc" id="L118"> return object.toString();</span> |
| } |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |