| <?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>SampleRealm.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 :: Samples :: Spring-Hibernate</a> > <a href="index.source.html" class="el_package">org.apache.shiro.samples.sprhib.security</a> > <span class="el_source">SampleRealm.java</span></div><h1>SampleRealm.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.samples.sprhib.security; |
| |
| import org.apache.shiro.authc.*; |
| import org.apache.shiro.authc.credential.Sha256CredentialsMatcher; |
| import org.apache.shiro.authz.AuthorizationInfo; |
| import org.apache.shiro.authz.SimpleAuthorizationInfo; |
| import org.apache.shiro.realm.AuthorizingRealm; |
| import org.apache.shiro.samples.sprhib.dao.UserDAO; |
| import org.apache.shiro.samples.sprhib.model.Role; |
| import org.apache.shiro.samples.sprhib.model.User; |
| import org.apache.shiro.subject.PrincipalCollection; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Component; |
| |
| /** |
| * The Spring/Hibernate sample application's one and only configured Apache Shiro Realm. |
| * |
| * <p>Because a Realm is really just a security-specific DAO, we could have just made Hibernate calls directly |
| * in the implementation and named it a 'HibernateRealm' or something similar.</p> |
| * |
| * <p>But we've decided to make the calls to the database using a UserDAO, since a DAO would be used in other areas |
| * of a 'real' application in addition to here. We felt it better to use that same DAO to show code re-use.</p> |
| */ |
| @Component |
| public class SampleRealm extends AuthorizingRealm { |
| |
| <span class="fc" id="L45"> protected UserDAO userDAO = null;</span> |
| |
| <span class="fc" id="L47"> public SampleRealm() {</span> |
| <span class="fc" id="L48"> setName("SampleRealm"); //This name must match the name in the User class's getPrincipals() method</span> |
| <span class="fc" id="L49"> setCredentialsMatcher(new Sha256CredentialsMatcher());</span> |
| <span class="fc" id="L50"> }</span> |
| |
| @Autowired |
| public void setUserDAO(UserDAO userDAO) { |
| <span class="fc" id="L54"> this.userDAO = userDAO;</span> |
| <span class="fc" id="L55"> }</span> |
| |
| protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { |
| <span class="fc" id="L58"> UsernamePasswordToken token = (UsernamePasswordToken) authcToken;</span> |
| <span class="fc" id="L59"> User user = userDAO.findUser(token.getUsername());</span> |
| <span class="pc bpc" id="L60" title="1 of 2 branches missed."> if( user != null ) {</span> |
| <span class="fc" id="L61"> return new SimpleAuthenticationInfo(user.getId(), user.getPassword(), getName());</span> |
| } else { |
| <span class="nc" id="L63"> return null;</span> |
| } |
| } |
| |
| |
| protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { |
| <span class="fc" id="L69"> Long userId = (Long) principals.fromRealm(getName()).iterator().next();</span> |
| <span class="fc" id="L70"> User user = userDAO.getUser(userId);</span> |
| <span class="pc bpc" id="L71" title="1 of 2 branches missed."> if( user != null ) {</span> |
| <span class="fc" id="L72"> SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();</span> |
| <span class="fc bfc" id="L73" title="All 2 branches covered."> for( Role role : user.getRoles() ) {</span> |
| <span class="fc" id="L74"> info.addRole(role.getName());</span> |
| <span class="fc" id="L75"> info.addStringPermissions( role.getPermissions() );</span> |
| <span class="fc" id="L76"> }</span> |
| <span class="fc" id="L77"> return info;</span> |
| } else { |
| <span class="nc" id="L79"> return null;</span> |
| } |
| } |
| |
| } |
| |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html> |