我是弹簧安全的新手。我了解有关身份验证管理器和其他内容的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中要添加什么?因此,它像在表单登录场景中一样将用户名与角色绑定在一起。)
请帮助您了解此工作流程?