如何使用Office 365身份验证配置Spring Security?

时间:2019-01-24 14:53:05

标签: java spring-mvc spring-security spring-security-oauth2

我是弹簧安全的新手。我了解有关身份验证管理器和其他内容的spring-security的基础知识。
我将angularjs用作前端,并将spring 3.0.5用作后端。

我想为现有项目添加基于角色的授权的Spring安全性。

我还想使用Office 365对用户进行身份验证。因此,我在https://apps.dev.microsoft.com/中创建了应用程序,并给了重定向URI作为我的本地主机操作。

使用我的代码,我能够使用Office 365进行身份验证,并能够在重定向操作中获取用户名。

@RequestMapping(value="/getCodeAndProcess", method=RequestMethod.POST)
    private ModelAndView getCodeAndProcess(@RequestParam("code") String code, HttpServletRequest request) throws Exception {
        HttpSession session = null;
        try {
        session = request.getSession(false);
        session.setMaxInactiveInterval(45*60);

        String username = helper.processCodeAndGetUsername(code); //this is I'm getting using office 365.
        String userRole = helper.getUserRoleBasedOnUsername(username);
        System.out.println("================userRole================="+userRole);
        if(username != "") {
            session.setAttribute("username", username);
            session.setAttribute("userRole", userRole);
        }else {
            session.invalidate();
            throw new Exception("username not found");
        }
       return new ModelAndView("redirect:/adminDashboard/showDashboard");
    }catch(Exception e) {
        log.error("Error while processing on users authentication.",e );
        session.invalidate();
        return new ModelAndView("redirect:/login/loginPage");
    }
}

现在,我不知道如何在security-context.xml中配置用户名和角色,因此我可以在应用程序中使用@PreAuthorize("hasRole('ROLE_USER')")isAuthenticated()<sec:authorize。 (在security-context.xml中要添加什么?因此,它像在表单登录场景中一样将用户名与角色绑定在一起。)

请帮助您了解此工作流程?

0 个答案:

没有答案