def do_login(self, username, password): username = self.sanitize(username) password = Sha256Hash(password, SALT_KEY, 2048).toBase64() user = _subject() session = user.getSession() if not user.isAuthenticated(): token = UsernamePasswordToken(username, password) token.setRememberMe(True) # By default, Shiro's session expire is 30 min. try: user.login(token) except IncorrectCredentialsException, e: return False
def do_login(self, username, password): password = Sha256Hash(password, SALT_KEY, 2048).toBase64() print password user = _subject() session = user.getSession() if not user.isAuthenticated(): token = UsernamePasswordToken(username, password) token.setRememberMe(True) try: user.login(token) except IncorrectCredentialsException, e: return False
//import org.junit.Assert; //import org.junit.Test; //import org.apache.shiro.mgt.SecurityManager; // //public class ShiroTest { // @Test // public void testHelloworld() { // // 1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager // Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); //采用自带的iniRealm验证. //// Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini"); //采用自定义的Realm验证. //// Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-multi-realm.ini"); //采用自定义的Realm验证. // // 2、得到SecurityManager实例 并绑定给SecurityUtils // SecurityManager securityManager = factory.getInstance(); // SecurityUtils.setSecurityManager(securityManager); // Subject subject = SecurityUtils.getSubject(); //// UsernamePasswordToken token = new UsernamePasswordToken("zhang", "1234"); // UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); //// UsernamePasswordToken token = new UsernamePasswordToken("wang", "123"); //// // // 二 角色认证 // //判断拥有角色:role1 // Assert.assertTrue(subject.hasRole("role1")); // //判断拥有角色:role1 and role2 // Assert.assertTrue(subject.hasAllRoles(Arrays.asList("role1", "role2"))); // //判断拥有角色:role1 and role2 and !role3 // boolean[] result = subject.hasRoles(Arrays.asList("role1", "role2", "role3")); // Assert.assertEquals(true, result[0]); // Assert.assertEquals(true, result[1]); // Assert.assertEquals(false, result[2]); // try { // // 4、登录,即身份验证