shiro教程

推荐先看--跟我学Shiro

级客学院


    org.apache.shiro
    shiro-all
    1.3.2




shiro教程_第1张图片
1.

shiro教程_第2张图片
2.

shiro教程_第3张图片
image

shiro教程_第4张图片
image

shiro教程_第5张图片
image

shiro教程_第6张图片
image

shiro教程_第7张图片
image

shiro教程_第8张图片
image

shiro教程_第9张图片
image

shiro教程_第10张图片
image

shiro教程_第11张图片
image

shiro教程_第12张图片
image
@Controller
public class HomeController {

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(method = RequestMethod.POST,value = "/")
    public String login(String userName, String password, RedirectAttributes redirectAttributes) {
        //Shiro方式登录
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(userName, password));
            return "redirect:/home";
        } catch (AuthenticationException ex) {
            ex.printStackTrace();
            redirectAttributes.addFlashAttribute("message","账号或密码错误");
            return "redirect:/";
        }
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(RedirectAttributes redirectAttributes) {
        //安全退出
        SecurityUtils.getSubject().logout();
        redirectAttributes.addFlashAttribute("message","你已安全退出");
        return "redirect:/";
    }


    @RequestMapping(value = "/home",method = RequestMethod.GET)
    public String home() {
        return "home";
    }

    @RequestMapping("/403")
    public String error403() {
        return "403";
    }
}

@Component
public class ShrioDbRealm extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 权限认证
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //返回当前登录的对象
        User user = (User) principalCollection.getPrimaryPrincipal();
        //获取当前对象拥有的角色
        List roleList = roleMapper.findByUserId(user.getId());
        if(!roleList.isEmpty()) {
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            for(Role role : roleList) {
                authorizationInfo.addRole(role.getRoleName());
            }
            return authorizationInfo;
        }
        return null;
    }

    /**
     * 登录认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        // 获取当前用户名
        String userName = usernamePasswordToken.getUsername();
        // n拿着用户名去数据库找
        User user = userMapper.findByUserName(userName);
        if(user != null) {
            return new SimpleAuthenticationInfo(user,user.getPassword(),getName());
        }
        // 一旦return null; 登录controller就会跑异常就会踢回去
        return null;
    }
}




    
        
        
    

    
    

    
        
        
        
        
        
        
        
        
        
            
                /static/** = anon
                /wx/** = anon
                /user = roles[role_admin]
                /setting/** = roles[role_admin]
                /** = authc
            
        
    




web.xml




    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        encodingFilter
        /*
    

    
    
        shiroFilter
        org.springframework.web.filter.DelegatingFilterProxy
    
    
        shiroFilter
        /*
    


    
    
        spring
        org.springframework.web.servlet.DispatcherServlet
        1
    
    
        spring
        /
    

    
    
        druid
        com.alibaba.druid.support.http.StatViewServlet
    
    
        druid
        /druid/*
    
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        contextConfigLocation
        classpath:applicationContext*.xml
    
    
    
    


你可能感兴趣的:(shiro教程)