Shiro基础功能之Servlet项目搭建

2018-03-01 11:19:07来源:oschina作者:久违了java人点击

分享
项目结构


1、创建mavn web工程

创建java 文件,并指定为Sources类型,过程略。


2、pom依赖


junit
junit
3.8.1
test



javax.servlet
javax.servlet-api
3.1.0
provided



log4j
log4j
1.2.17



org.slf4j
slf4j-api
1.7.25



javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided



commons-logging
commons-logging
1.2



org.apache.shiro
shiro-core
1.4.0



org.apache.shiro
shiro-web
1.4.0


3、shiro.ini文件,文件的名称与存放路径说明:点击访问
[main]
authc.loginUrl=/login
roles.unauthorizedUrl=/unauthorized.jsp
perms.unauthorizedUrl=/unauthorized.jsp
[users]
java1234=123456,admin
jack=123,teacher
marry=234
json=345
[roles]
admin=user:*
teacher=student:*
[urls]
/login=anon
/admin=authc
/student=roles[teacher]
/teacher=perms["user:create"]
4、log4j.properties
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
5、创建Servlet类

AdminServletLoginServlet


public class AdminServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("admin do get");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("admin do post");
}
}
package com.ssm.servlet;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* /* Created with IntelliJ IDEA.
* /* User: wugong.jie
* /* Date: 2018/2/27 9:07
* /* To change this template use File | Settings | File Templates.
* /* Description:
* /
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("login doget");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("login dopost");
String userName=request.getParameter("userName");
String password=request.getParameter("password");
Subject subject= SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
try{
subject.login(token);
response.sendRedirect("success.jsp");
}catch(Exception e){
e.printStackTrace();
request.setAttribute("errorInfo", "用户名或者密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
6、web.xml,不要使用mavn创建的web.xml,直接替换为我的就可以
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
ShiroTest


org.apache.shiro.web.env.EnvironmentLoaderListener



ShiroFilter
org.apache.shiro.web.servlet.ShiroFilter


ShiroFilter
/*


loginServlet
com.ssm.servlet.LoginServlet


adminServlet
com.ssm.servlet.AdminServlet


loginServlet
/login


adminServlet
/admin


index.jsp

7、创建jsp

login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here



userName:

password:





success.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here


欢迎你!


unauthorized.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here


认证未通过,或者权限不足


8、测试访问

http://localhost:8280/login


http://localhost:8280/admin 不登录直接访问此路径会跳转到/login


访问权限的控制正在研究中.....


工程下载地址:链接:https://pan.baidu.com/s/1snn2lhb 密码:gxmg


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台