- 1、依赖
- 2、自定义的realm
- 3、shiro的配置类
- 4、控制器
- 5、 登陆界面
- 1.pom文件
org.apache.shiro shiro-spring 1.3.2
#2、自定义的realm
package com.gupaoedu.realm;
import com.gupaoedu.pojo.User;
import com.gupaoedu.service.IUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.SimpleByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class AuthcRealm extends AuthorizingRealm {
@Autowired
private IUserService service;
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String userName = token.getUsername();
System.out.println("开始认证:" + userName);
User user = new User();
user.setUsername(userName);
// 根据账号认证
List list = service.query(user);
if(list == null || list.size() != 1){
// 账号不存在或者异常
return null;
}
user = list.get(0);
return new SimpleAuthenticationInfo(user
,user.getPassword() // 密码
,new SimpleByteSource(user.getSalt()) // salt
,"authcRealm" // 自定义的Realm名称
);
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
}
3、shiro的配置类
package com.gupaoedu.config;
import com.gupaoedu.realm.AuthcRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
// 散列算法
private String hashAlgorithmName = "md5";
// 迭代次数
private Integer hashIterations = 1024;
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher(){
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
matcher.setHashAlgorithmName(hashAlgorithmName);
matcher.setHashIterations(hashIterations);
return matcher;
}
@Bean
public AuthcRealm authcRealm(HashedCredentialsMatcher matcher){
AuthcRealm realm = new AuthcRealm();
realm.setCredentialsMatcher(matcher);
return realm;
}
@Bean
public SecurityManager securityManager(AuthcRealm realm){
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
manager.setRealm(realm);
return manager;
}
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager manager){
ShiroFilterFactoryBean filter = new ShiroFilterFactoryBean();
filter.setSecurityManager(manager);
filter.setLoginUrl("/login.do");
filter.setSuccessUrl("/success.html");
filter.setUnauthorizedUrl("/refuse.html");
// 设置过滤器链
Map map = new HashMap<>();
map.put("/css
@Controller
public class AuthcController {
@RequestMapping("/login.do")
public String login(HttpServletRequest request){
// 认证失败的异常信息
Object obj = request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
System.out.println("认证失败的信息:" + obj);
return "login";
}
@RequestMapping("/logout.do")
public String logout(){
SecurityUtils.getSubject().logout();
return "redirect:/login";
}
}
5、登陆界面
Title
登录管理



