packagecom.bjsxt.realm;importorg.apache.shiro.authc.AuthenticationException;importorg.apache.shiro.authc.AuthenticationInfo;importorg.apache.shiro.authc.AuthenticationToken;importorg.apache.shiro.authc.SimpleAuthenticationInfo;importorg.apache.shiro.realm.AuthenticatingRealm;importorg.apache.shiro.util.ByteSource;publicclassMyRealmextendsAuthenticatingRealm{@OverrideprotectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationToken token)throwsAuthenticationException{System.out.println("执行认证");//用户名String username = token.getPrincipal().toString();//密码String pwd =newString((char[])token.getCredentials());System.out.println(username+" "+pwd);//先从数据库查询select * from user where username=? 查看用户名是否存在if(username.equals("admin")){//假设用户名为admin时能从数据库中查询出来//根据之前查询出来的用户信息获取密码。假设查询出来的密码是pwdString password="pwd";//此处需要注意,第二个参数是从数据库查询出来的密码,而不是传递过来的密码。//第三个参数自定义。但是尽量不重复了。常直接使用用户名当做realname名字。AuthenticationInfo info =newSimpleAuthenticationInfo(token.getPrincipal(),password,"realmname");//shiro会判断从数据库查询出来的密码和客户端传递过来的密码是否一致。return info;}//返回null说明用户名不存在。returnnull;}}
packagecom.bjsxt.realm;importorg.apache.shiro.authc.AuthenticationException;importorg.apache.shiro.authc.AuthenticationInfo;importorg.apache.shiro.authc.AuthenticationToken;importorg.apache.shiro.authc.SimpleAuthenticationInfo;importorg.apache.shiro.realm.AuthenticatingRealm;importorg.apache.shiro.util.ByteSource;publicclassMyRealmextendsAuthenticatingRealm{@OverrideprotectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationToken token)throwsAuthenticationException{System.out.println("执行认证");//用户名String username = token.getPrincipal().toString();//密码String pwd =newString((char[])token.getCredentials());System.out.println(username+" "+pwd);//先从数据库查询select * from user where username=? 查看用户名是否存在if(username.equals("admin")){//假设用户名为admin时能从数据库中查询出来//密码必须是MD5加密后的密码(自行更改)String password="7614fd642608ca0755b78d2b2c352e19";//假设id是从数据库中取出的idLong id =123L;//第三个参数是加密的盐AuthenticationInfo info =newSimpleAuthenticationInfo(token.getPrincipal(), password,ByteSource.Util.bytes(“加密使用的值”),token.getPrincipal().toString());//shiro会判断从数据库查询出来的密码和客户端传递过来的密码是否一致。return info;}//返回null说明用户名不存在。returnnull;}}