Example usage for org.apache.shiro.realm SimpleAccountRealm SimpleAccountRealm

List of usage examples for org.apache.shiro.realm SimpleAccountRealm SimpleAccountRealm

Introduction

In this page you can find the example usage for org.apache.shiro.realm SimpleAccountRealm SimpleAccountRealm.

Prototype

public SimpleAccountRealm(String name) 

Source Link

Usage

From source file:com.monkeyk.os.web.ShiroTest.java

License:Open Source License

@Test(enabled = false)
public void login() {
    String username = "abc";
    //init SecurityManager
    SimpleAccountRealm realm = new SimpleAccountRealm("simple-realm");
    realm.addAccount(username, "abc", "USER");

    SimpleAccountRealm realm2 = new SimpleAccountRealm("simple-realm2");
    realm2.addAccount(username, "abc", "USER", "ADMIN");

    List<Realm> realmList = new ArrayList<>();
    realmList.add(realm);/*from www .  j  a v a2  s  .c  o  m*/
    realmList.add(realm2);

    SecurityManager securityManager = new DefaultSecurityManager(realmList);
    SecurityUtils.setSecurityManager(securityManager);

    UsernamePasswordToken token = new UsernamePasswordToken(username, "abcdd");

    final Subject subject = SecurityUtils.getSubject();
    subject.login(token);

    final Subject subject1 = SecurityUtils.getSubject();
    assertTrue(subject1.isAuthenticated());

    assertFalse(subject1.isPermitted("OK"));
    assertTrue(subject1.hasRole("USER"));

    //        assertTrue(subject1.isPermitted("USER:c,u"));

}

From source file:org.ops4j.pax.shiro.cdi.AbstractCdiTest.java

License:Apache License

@BeforeClass
public static void start() throws Exception {
    securityManager = new DefaultSecurityManager();
    SecurityUtils.setSecurityManager(securityManager);

    realm = new SimpleAccountRealm("test-realm");
    realm.addRole("role");
    realm.addAccount("foo", "bar", "role");
    realm.addAccount("bilbo", "precious", "hobbit");
    realm.setRolePermissionResolver(new RolePermissionResolver() {
        public Collection<Permission> resolvePermissionsInRole(String roleString) {
            if ("role".equals(roleString)) {
                final Permission dp = new WildcardPermission("permission");
                return Arrays.asList(dp);
            }/*from   ww  w . j  a  va  2  s. c o m*/
            return Collections.emptyList();
        }
    });
    securityManager.setRealm(realm);
}