Feladatok:
>java
%%loadFromPOM
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.26</version>
</dependency>
>bash
vimcat shiro.ini
>java
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
Factory<SecurityManager> factory = new IniSecurityManagerFactory("shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
System.out.println("Retrieved the correct value! [" + value + "]");
}
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(false);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
System.out.println("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
System.out.println("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
catch (AuthenticationException ae) {
//unexpected condition? error?
}
}
System.out.println("User [" + currentUser.getPrincipal() + "] logged in successfully.");
if (currentUser.hasRole("schwartz")) {
System.out.println("May the Schwartz be with you!");
} else {
System.out.println("Hello, mere mortal.");
}
if (currentUser.isPermitted("lightsaber:weild")) {
System.out.println("You may use a lightsaber ring. Use it wisely.");
} else {
System.out.println("Sorry, lightsaber rings are for schwartz masters only.");
}
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
System.out.println("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
System.out.println("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
currentUser.logout();
>java
%%loadFromPOM
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.46.0.1</version>
</dependency>
>java
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.apache.shiro.realm.jdbc.JdbcRealm;
public class DBRealm extends JdbcRealm {
public DBRealm() {
super();
super.setRolePermissionResolver(new RolePermissionResolver() {
@Override
public Collection<Permission> resolvePermissionsInRole(String roleString) {
Collection<Permission> perms = Collections.emptySet();
try {
Set<String> permss = getPermissions(dataSource.getConnection(), roleString);
perms = new LinkedHashSet<Permission>(permss.size());
for (String perm : permss) {
perms.add(new WildcardPermission(perm));
}
} catch (SQLException e) {
e.printStackTrace();
}
return perms;
}
});
}
protected Set<String> getPermissions(Connection conn, String roleString) throws SQLException {
List<String> roleNames = new ArrayList<String>();
roleNames.add(roleString);
return super.getPermissions(conn, "", roleNames);
}
}
>bash
vimcat sqlite-users.sql
>bash
sqlite3 light.db "select * from users;"
echo "--------------------------------"
sqlite3 light.db "select * from user_roles;"
echo "--------------------------------"
sqlite3 light.db "select * from roles_permissions;"
>java
import org.apache.shiro.authc.credential.DefaultPasswordService;
PasswordService pwdService = new DefaultPasswordService();
pwdService.encryptPassword("vespa3");
>java
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordMatcher;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm.SaltStyle;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.sqlite.SQLiteConfig;
PasswordService pwdService = new DefaultPasswordService();
PasswordMatcher pwdMatcher = new PasswordMatcher();
pwdMatcher.setPasswordService(pwdService);
SQLiteConfig config = new SQLiteConfig();
config.enforceForeignKeys(true);
DBRealm realm = new DBRealm();
org.sqlite.SQLiteDataSource ds = new org.sqlite.SQLiteDataSource(config);
ds.setUrl("jdbc:sqlite:light.db");
realm.setDataSource(ds);
realm.setCredentialsMatcher(pwdMatcher);
realm.setSaltStyle(SaltStyle.COLUMN);
SecurityManager securityManager = new DefaultSecurityManager(realm);
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
System.out.println("Retrieved the correct value! [" + value + "]");
}
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa3");
token.setRememberMe(false);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
System.out.println("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
System.out.println("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
catch (AuthenticationException ae) {
//unexpected condition? error?
}
}
System.out.println("User [" + currentUser.getPrincipal() + "] logged in successfully.");
if (currentUser.hasRole("schwartz")) {
System.out.println("May the Schwartz be with you!");
} else {
System.out.println("Hello, mere mortal.");
}
if (currentUser.isPermitted("lightsaber:weild")) {
System.out.println("You may use a lightsaber ring. Use it wisely.");
} else {
System.out.println("Sorry, lightsaber rings are for schwartz masters only.");
}
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
System.out.println("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
System.out.println("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
currentUser.logout();