JavaWeb同步学习笔记之七十八、JavaWeb_权限管理代码实现

JavaWeb_权限管理代码实现

权限管理代码实现

  • 1.authority-manager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<center>
		
		<br><br>
		<form action="/Filter_1/authorityServlet?method=getAuthority" method="post">
			username:<input type="text" name="userName"/>
			<input type="submit" value="Submit">
		</form>
		
		<c:if test="${requestScope.user != null }">
			<br><br>
			
			${requestScope.user.userName }的权限是:
			<br><br>
			
			<form action="/Filter_1/authorityServlet?method=updateAuthority" method="post">
				
				<input type="hidden" name="userName" value="${requestScope.user.userName }"/>
				
				<c:forEach items="${authorities }" var="auth">
				<c:set var="flag" value="false"></c:set>
					
					<c:forEach items="${user.authorities }" var="ua">
						<c:if test="${ua.url == auth.url }">
							<c:set var="flag" value="true"></c:set>
						</c:if>
					</c:forEach>
					<c:if test="${flag == true }">
						<input type="checkbox" name="authority" value="${auth.url }" checked="checked" />${auth.displayName }
					</c:if>
					<c:if test="${flag == false }">
						<input type="checkbox" name="authority" value="${auth.url }" />${auth.displayName }
					</c:if>
					<br><br>
					
				</c:forEach>
				
				<input type="submit" value="Update">
				
			</form>
			
			<br><br>
		</c:if>
		
	</center>		

</body>
</html>
  • 2.Authority.java
/**  
 * All rights Reserved,Designed By XS
 * @Title: Authority.java
 * @Package com.xs.javaweb
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月18日 上午10:43:22
 * @version V1.0
 */
package com.xs.javaweb;

/**   
 * @ClassName: Authority
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月18日 上午10:43:22
 * @version V1.0
 */
public class Authority {
	
	private String displayName;
	
	private String url;

	/**  
	 * @return the displayName
	 */
	public String getDisplayName() {
		return displayName;
	}

	/**  
	 * @param displayName: the displayName to set
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**  
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**  
	 * @param url: the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**   
	 * @Title: Authority
	 * @Description: TODO
	 * @param displayName
	 * @param url
	 */
	public Authority(String displayName, String url) {
		super();
		this.displayName = displayName;
		this.url = url;
	}

	/**   
	 * @Title: Authority
	 * @Description: TODO
	 */
	public Authority() {
		super();
	}
	
}
  • 3.User.java
/**  
 * All rights Reserved,Designed By XS
 * @Title: User.java
 * @Package com.xs.javaweb
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月18日 上午10:43:33
 * @version V1.0
 */
package com.xs.javaweb;

import java.util.List;

/**   
 * @ClassName: User
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月18日 上午10:43:33
 * @version V1.0
 */
public class User {
	
	private String userName;
	
	private List<Authority> authorities;

	/**  
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**  
	 * @param userName: the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**  
	 * @return the authorities
	 */
	public List<Authority> getAuthorities() {
		return authorities;
	}

	/**  
	 * @param authorities: the authorities to set
	 */
	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}

	/**   
	 * @Title: User
	 * @Description: TODO
	 * @param userName
	 * @param authorities
	 */
	public User(String userName, List<Authority> authorities) {
		super();
		this.userName = userName;
		this.authorities = authorities;
	}

	/**   
	 * @Title: User
	 * @Description: TODO
	 */
	public User() {
		super();
	}
	
}
  • 4.UserDao.java
/**  
 * All rights Reserved,Designed By XS
 * @Title: UserDAO.java
 * @Package com.xs.javaweb
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月18日 上午10:46:28
 * @version V1.0
 */
package com.xs.javaweb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserDAO
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月18日 上午10:46:28
 * @version V1.0
 */
public class UserDAO {

	private static Map<String, User> users;

	private static List<Authority> authorities = null;

	static {

		authorities = new ArrayList<>();

		authorities.add(new Authority("Article-1", "app/article-1.jsp"));
		authorities.add(new Authority("Article-2", "app/article-2.jsp"));
		authorities.add(new Authority("Article-3", "app/article-3.jsp"));
		authorities.add(new Authority("Article-4", "app/article-4.jsp"));

		users = new HashMap<String, User>();

		User user1 = new User("AAA", authorities.subList(0, 2));
		users.put("AAA", user1);

		User user2 = new User("BBB", authorities.subList(2, 4));
		users.put("BBB", user2);

	}

	public User get(String userName) {
		return users.get(userName);
	}

	public void update(String userName, List<Authority> authorities) {
		users.get(userName).setAuthorities(authorities);

	}
	
	public List<Authority> getAuthorities() {
		return authorities;
	}

	/**   
	 * @Title: getAuthorities
	 * @Description: TODO
	 * @param authorities2
	 * @return 
	 * @return List<Authority>
	 */
	public List<Authority> getAuthorities(String[] urls) {
		List<Authority> authorities2 = new ArrayList<>();
		
		for (Authority authority: authorities) {
			if (urls != null) {
				for (String url: urls) {
					if (url.equals(authority.getUrl())) {
						authorities2.add(authority);
					}
					
				}
			}
			
		}
		
		return authorities2;
	}

}
  • 5.AuthorityServlet.java
package com.xs.javaweb;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/authorityServlet")
public class AuthorityServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * <p>
	 * Title: doGet
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	/**
	 * <p>
	 * Title: doPost
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String methodName = request.getParameter("method");
		
		
		try {
			Method method = getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private UserDAO userDAO = new UserDAO();

	/**
	 * @Title: getAuthority
	 * @Description: TODO
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return void
	 */
	public void getAuthority(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String userName = request.getParameter("userName");
		User user = userDAO.get(userName);

		request.setAttribute("user", user);
		request.setAttribute("authorities", userDAO.getAuthorities());
		request.getRequestDispatcher("/app/authority-manager.jsp").forward(request, response);

	}

	public void updateAuthority(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String userName = request.getParameter("userName");
		
		String [] authorities = request.getParameterValues("authority");
		
		List<Authority> authorityList = userDAO.getAuthorities(authorities);
		
		
		
		userDAO.update(userName, authorityList);
		
		response.sendRedirect(request.getContextPath() + "/app/authority-manager.jsp");
		
		
	}

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值