Spring Security学习——基于角色的权限验证

                        基于角色的权限验证

       在现实世界中,不能的角色对应着不同的权限,比如说用户可以访问用户的模板,但不能访问到后台,管理员可以访问到后台的管理模板,而只有数据库管理员才能直接访问到数据底层管理,比如说一些登录日志和私密数据。Spring Security框架支持基于角色的权限验证,可以在系统中添加多种角色,用户权限验证登陆时,就可以根据输入的登陆信息,判断出输入的用户需要哪个角色,然后根据相应的业务逻辑让此用户访问哪些资源。如果单纯用代码来实现角色权限控制的话,业务逻辑将会异常复杂,从而导致整个系统难以维护和扩展,而且如果需要修改用户权限或者修改相应业务的话,就要修改源代码,这样的维护成本会相当的高。而使用Spring Security框架来作为角色权限控制的解决方案,可以在最大化扩展能力的前提下轻松实现权限控制的业务功能,并且代码友好。
      如下的例子是要根据第一个例子改造而来,在系统中添加了数据库管理员的角色,如果要访问数据库的资源的话,就必须使用数据库管理员的用户登录,而使用普通用户登陆的话就会告知没有权限。当验证成功后,就会将由AuthoritySuccessHandler处理,这个处理器可以根据登陆的用户角色来决定转向哪个请求,相应起于一个权限分发器的作用。
在security配置文件中,在login-form中配置authentication-success-handler-ref为AutoritySuccessHandler的一个Beean对象,然后参加多个角色,就可以实现多角色权限管理。

package security.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

@Component
public class AuthoritySuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

	@Override
	protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		String targetURL = determineTargetUrl(authentication);
		if (response.isCommitted()) {
			return;
		}
		redirectStrategy.sendRedirect(request, response, targetURL);
	}

	private String determineTargetUrl(Authentication authentication) {
		String url = "";
		Collection authorities = authentication.getAuthorities();
		List roles = new ArrayList();
		for (GrantedAuthority authority : authorities) {
			roles.add(authority.getAuthority());
		}
		System.out.println(roles);
		if (roles.contains("ROLE_DBA")) {
			url = "/dba";
		} else if (roles.contains("ROLE_USER")) {
			url = "/user";
		}
		return url;
	}
}



	
	

		
		
		
		
		
	

	
		
			
				
				
			
		
	

在Controller中如下逻辑
package security.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

	@RequestMapping(value = "", method = { RequestMethod.GET })

	public ModelAndView welcomePage() {
		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security Study");
		model.addObject("message", "welcome!!");
		model.setViewName("hello");
		return model;
	}

	@RequestMapping(value = "/admin**", method = RequestMethod.GET)
	public ModelAndView adminPage() {
		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security Hello World");
		model.addObject("message", "This is protected page!");
		model.setViewName("admin");

		return model;

	}

	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public ModelAndView user() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("user");
		return mav;
	}

	@RequestMapping(value = "/dba", method = RequestMethod.GET)
	public ModelAndView dba() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("dba");
		return mav;
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(@RequestParam(value = "error", required = false) String error,
			@RequestParam(value = "logout", required = false) String logout) {

		ModelAndView model = new ModelAndView();
		if (error != null) {
			model.addObject("error", "Invalid username and password!");
		}

		if (logout != null) {
			model.addObject("msg", "You've been logged out successfully.");
		}
		model.setViewName("login");

		return model;

	}
}






你可能感兴趣的:(javaweb,学习,Spring)