blob: 59bbfacc4674352ea9ab43c7af611c6fbffb5268 [file] [log] [blame]
<?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> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.samples.sprhib.security</a> &gt; <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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.
*
* &lt;p&gt;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.&lt;/p&gt;
*
* &lt;p&gt;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.&lt;/p&gt;
*/
@Component
public class SampleRealm extends AuthorizingRealm {
<span class="nc" id="L45"> protected UserDAO userDAO = null;</span>
<span class="nc" id="L47"> public SampleRealm() {</span>
<span class="nc" id="L48"> setName(&quot;SampleRealm&quot;); //This name must match the name in the User class's getPrincipals() method</span>
<span class="nc" id="L49"> setCredentialsMatcher(new Sha256CredentialsMatcher());</span>
<span class="nc" id="L50"> }</span>
@Autowired
public void setUserDAO(UserDAO userDAO) {
<span class="nc" id="L54"> this.userDAO = userDAO;</span>
<span class="nc" id="L55"> }</span>
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
<span class="nc" id="L58"> UsernamePasswordToken token = (UsernamePasswordToken) authcToken;</span>
<span class="nc" id="L59"> User user = userDAO.findUser(token.getUsername());</span>
<span class="nc bnc" id="L60" title="All 2 branches missed."> if( user != null ) {</span>
<span class="nc" 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="nc" id="L69"> Long userId = (Long) principals.fromRealm(getName()).iterator().next();</span>
<span class="nc" id="L70"> User user = userDAO.getUser(userId);</span>
<span class="nc bnc" id="L71" title="All 2 branches missed."> if( user != null ) {</span>
<span class="nc" id="L72"> SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();</span>
<span class="nc bnc" id="L73" title="All 2 branches missed."> for( Role role : user.getRoles() ) {</span>
<span class="nc" id="L74"> info.addRole(role.getName());</span>
<span class="nc" id="L75"> info.addStringPermissions( role.getPermissions() );</span>
<span class="nc" id="L76"> }</span>
<span class="nc" 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.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>