weixin_37572849 2020-08-03 19:28 采纳率: 0%
浏览 290

在登录跳转到主页面给拦截器拦截了

/**

  • 表示用户没有登录就访问受保护资源时抛出的异常
  • @author Lenovo
    *
    */
    public class AccessForbiddenException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public AccessForbiddenException() {
    super();
    }

    public AccessForbiddenException(String message, Throwable cause, boolean enableSuppression,
    boolean writableStackTrace) {
    super(message, cause, enableSuppression, writableStackTrace);
    }

    public AccessForbiddenException(String message, Throwable cause) {
    super(message, cause);
    }

    public AccessForbiddenException(String message) {
    super(message);
    }

    public AccessForbiddenException(Throwable cause) {
    super(cause);
    }

}

/**

  • 登录失败后抛出的异常
  • @author Lenovo
    *
    */
    public class LoginFailedException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public LoginFailedException() {
    super();
    }

    public LoginFailedException(String message, Throwable cause, boolean enableSuppression,
    boolean writableStackTrace) {
    super(message, cause, enableSuppression, writableStackTrace);
    }

    public LoginFailedException(String message, Throwable cause) {
    super(message, cause);
    }

    public LoginFailedException(String message) {
    super(message);
    }

    public LoginFailedException(Throwable cause) {
    super(cause);
    }

}

@Controller
public class AdminHandler {

@Autowired
private AdminService adminService;

@RequestMapping("/admin/do/logout.html")
public String doLogout(HttpSession session) {

    // 强制 Session 失效
    session.invalidate();

    return "redirect:/admin/to/login/page.html";


}

@RequestMapping(value="/admin/do/login.html")
public String doLogin(
    @RequestParam("loginAcct") String loginAcct,
    @RequestParam("userPswd") String userPswd,
    HttpSession session
) {

    //调用Service方法执行登录检查
    // 这个方法如果能够返回 admin 对象说明登录成功,如果账号、密码不正确则会抛出异常
    Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);

    // 将登录成功返回的 admin 对象存入 Session 域
    session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);

//  return "admin-main";
    //重定向到主页面
    return "redirect:/admin/to/main/page.html";
}

}

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- 配置自动扫描的包:扫描handler -->
<context:component-scan base-package="com.atguigu.crowd.mvc"/>

<!-- 配置SpringMVC的注解驱动 -->
<mvc:annotation-driven/>

<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<!-- 配置基于XML的异常映射 -->
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <!-- 配置异常类型和具体视图页面的对应关系 -->
    <property name="exceptionMappings">
        <props>
            <!-- key属性指定异常全类名 -->
            <!-- 标签体中写对应的视图(这个值要拼前后缀得到具体路径) -->
             <prop key="java.lang.Exception">system-error</prop> 
            <!-- <prop key="com.atguigu.crowd.exception.AccessForbiddenException">admin-login</prop> -->
        </props>
    </property>
</bean>

<!-- 配置view-controller,直接把请求地址和视图名称关联起来,不必写handler方法了 -->
<!-- 
    @RequestMapping("/admin/to/login/page.html")
    public String toLoginPage(){
        return "admin-login";
    }
 -->
<mvc:view-controller path="/admin/to/login/page.html" view-name="admin-login"/>
<mvc:view-controller path="/admin/to/main/page.html" view-name="admin-main"/>

<!-- 注册拦截器 -->
<mvc:interceptors>
    <mvc:interceptor>
        <!-- mvc:mapping配置要拦截的资源 -->
        <!-- /*对应一层路径,比如:/aaa -->
        <!-- /**对应多层路径,比如:/aaa/bbb或/aaa/bbb/ccc或/aaa/bbb/ccc/ddd -->
        <mvc:mapping path="/**"/>

        <!-- mvc:exclude-mapping配置不拦截的资源 -->
        <mvc:exclude-mapping path="/admin/to/login/page.html"/>
        <mvc:exclude-mapping path="/admin/do/login.html"/>
        <mvc:exclude-mapping path="/admin/do/logout.html"/>

        <!-- 配置拦截器类 -->
        <bean class="com.atguigu.crowd.mvc.interceptor.LoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

[19:14:17.084] [DEBUG] [http-nio-8080-exec-5] [org.springframework.web.servlet.DispatcherServlet] [POST "/projectcrowd/admin/do/login.html", parameters={masked}]
[19:14:17.096] [DEBUG] [http-nio-8080-exec-5] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] [Mapped to public java.lang.String com.atguigu.crowd.mvc.handler.AdminHandler.doLogin(java.lang.String,java.lang.String,javax.servlet.http.HttpSession)]
[19:14:17.240] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager] [Creating new transaction with name [com.atguigu.crowd.service.impl.AdminServiceImpl.getAdminByLoginAcct]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly]
[19:14:17.289] [INFO ] [http-nio-8080-exec-5] [com.alibaba.druid.pool.DruidDataSource] [{dataSource-1} inited]
[19:14:17.563] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager] [Acquired Connection [com.mysql.jdbc.Connection@76deec82] for JDBC transaction]
[19:14:17.572] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceUtils] [Setting JDBC Connection [com.mysql.jdbc.Connection@76deec82] read-only]
[19:14:17.572] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager] [Switching JDBC Connection [com.mysql.jdbc.Connection@76deec82] to manual commit]
[19:14:17.588] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.SqlSessionUtils] [Creating a new SqlSession]
[19:14:17.604] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.SqlSessionUtils] [Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@685f93e9]]
[19:14:17.700] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.transaction.SpringManagedTransaction] [JDBC Connection [com.mysql.jdbc.Connection@76deec82] will be managed by Spring]
[19:14:17.709] [DEBUG] [http-nio-8080-exec-5] [com.atguigu.crowd.mapper.AdminMapper.selectByExample] [==> Preparing: select id, login_acct, user_pswd, user_name, email, create_time from t_admin WHERE ( login_acct = ? ) ]
[19:14:17.735] [DEBUG] [http-nio-8080-exec-5] [com.atguigu.crowd.mapper.AdminMapper.selectByExample] [==> Parameters: jerry(String)]
[19:14:17.753] [DEBUG] [http-nio-8080-exec-5] [com.atguigu.crowd.mapper.AdminMapper.selectByExample] [<== Total: 1]
[19:14:17.754] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.SqlSessionUtils] [Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@685f93e9]]
[19:14:17.757] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.SqlSessionUtils] [Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@685f93e9]]
[19:14:17.757] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.SqlSessionUtils] [Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@685f93e9]]
[19:14:17.757] [DEBUG] [http-nio-8080-exec-5] [org.mybatis.spring.SqlSessionUtils] [Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@685f93e9]]
[19:14:17.757] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager] [Initiating transaction commit]
[19:14:17.758] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager] [Committing JDBC transaction on Connection [com.mysql.jdbc.Connection@76deec82]]
[19:14:17.760] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceUtils] [Resetting read-only flag of JDBC Connection [com.mysql.jdbc.Connection@76deec82]]
[19:14:17.761] [DEBUG] [http-nio-8080-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager] [Releasing JDBC Connection [com.mysql.jdbc.Connection@76deec82] after transaction]
[19:14:17.772] [DEBUG] [http-nio-8080-exec-5] [org.springframework.web.servlet.view.RedirectView] [View name 'redirect:', model {}]
[19:14:17.773] [DEBUG] [http-nio-8080-exec-5] [org.springframework.web.servlet.DispatcherServlet] [Completed 302 FOUND]
[19:14:17.780] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.DispatcherServlet] [GET "/projectcrowd/admin/to/main/page.html", parameters={}]
[19:14:17.783] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] [Mapped to ParameterizableViewController [view="admin-main"]]
[19:14:17.786] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] [Resolving to view 'system-error' based on mapping [java.lang.Exception]]
[19:14:17.786] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] [Resolved [com.atguigu.crowd.exception.AccessForbiddenException: 请登录以后再访问] to ModelAndView [view="system-error"; model={exception=com.atguigu.crowd.exception.AccessForbiddenException: 请登录以后再访问}]]
[19:14:17.786] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.DispatcherServlet] [Using resolved error view: ModelAndView [view="system-error"; model={exception=com.atguigu.crowd.exception.AccessForbiddenException: 请登录以后再访问}]]
[19:14:17.789] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.view.JstlView] [View name 'system-error', model {exception=com.atguigu.crowd.exception.AccessForbiddenException: 请登录以后再访问}]
[19:14:17.800] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.view.JstlView] [Forwarding to [/WEB-INF/system-error.jsp]]
[19:14:18.084] [DEBUG] [http-nio-8080-exec-6] [org.springframework.web.servlet.DispatcherServlet] [Completed 200 OK]

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-08-04 09:51
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。