SpringBoot - shiro 作登录认证和拦截

SpringBoot - shiro 作登录认证和拦截

Controller

/**
 * @author Shu
 * @date 2020/3/26 上午11:45
 */

@Api (tags = "管理系统登录页面", value = "账号密码认证")
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public BaseResponseUtil login(@RequestParam("userName") String userName, @RequestParam ("password") String password){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        try {
            subject.login(token);
            return DataResponseUtil.success(CodeEnum.SUCCESS,"登录成功");
        }catch (UnknownAccountException e){
            return DataResponseUtil.error (CodeEnum.BAD_REQUEST, "用户名或密码错误");
        }catch (IncorrectCredentialsException e){
            return DataResponseUtil.error(CodeEnum.BAD_REQUEST,"用户名或密码错误");
        }

    }

}

ShiroConfig

/**
 * @author Shu
 * @date 2020/4/14 上午9:13
 */

@Configuration
public class ShiroConfig {


    //shiroFilterFactoryBean:`````3``````
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //添加shiro的内置过滤器
        /*
        * anon:无须认证即可访问
        * authc:必须认证才可以访问
        * user:必须有  记住我 功能  才能用
        * perms:拥有对某个资源的权限才可以访问
        * role:拥有某个角色权限才可以访问
        * */

        //拦截
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("*/login","anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        //设置登录页面
        shiroFilterFactoryBean.setLoginUrl("/login");


        return shiroFilterFactoryBean;
    }
    
    //DefaultWebSecurityManager:`````2``````
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

    //Realm对象,需要自定义:`````1``````
    @Bean(name = "userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }
}

UserRealm

/**
 * @author Shu
 * @date 2020/4/14 上午9:06
 */


public class UserRealm extends AuthorizingRealm {

    @Autowired
    private LoginService loginService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String username = ((UsernamePasswordToken) token).getUsername();
        Login login = loginService.selectUserByName(username);
        if (login==null){
            return null;
        }
        String password = login.getPassword();
        String realmName = getName();
        //        盐值加密
        ByteSource credentialsSalt = ByteSource.Util.bytes(username);
        SimpleAuthenticationInfo authcInfo = new SimpleAuthenticationInfo(username, password,credentialsSalt,realmName);

        return authcInfo;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值