成功和失败的跳转Handler

配置插件

package com.imoke.web.Async;

import com.aimuke.security.brewoser.anth.AuthenticationUnsucessFulHandler;
import com.aimuke.security.brewoser.anth.ImoocAuthenticationSuccessHandler;
import com.imoke.security.SecurityProperties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class brwoserConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private SecurityProperties securityProperties;


    @Autowired
    private ImoocAuthenticationSuccessHandler imoocAuthenticationSuccessHandler;
    @Autowired
    private AuthenticationUnsucessFulHandler authenticationUnsucessFulHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
/* http.httpBasic()*/
                .loginPage("/users")
                .loginProcessingUrl("/users/loginuser")
                .successHandler(imoocAuthenticationSuccessHandler)
                .failureHandler(authenticationUnsucessFulHandler)
                .and()
                .csrf().disable()//关闭csrf
                .authorizeRequests()
                .antMatchers("/users", securityProperties.browser.getLoginPage()).permitAll()//当是这个页面是不需要身份认证
                //授权认证
                .anyRequest()//任何请求
                .authenticated();//都需要安全认证
        /*username-parameter:表示登录时用户名使用的是哪个参数,默认是“j_username”。
        password-parameter:表示登录时密码使用的是哪个参数,默认是“j_password”。
        login-processing-url:表示登录时提交的地址,默认是“/j-spring-security-check”。这个只是Spring Security用来标记登录页面使用的提交地址,真正关于登录这个请求是不需要用户自己处理的
    */
    }
}

 

失败后的

package com.aimuke.security.brewoser.anth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.imoke.security.SecurityProperties.SecurityProperties;
import com.imoke.security.SecurityProperties.properties.LoginResponseType;
import com.imoke.web.support.SimpleResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Slf4j
public class AuthenticationUnsucessFulHandler extends SimpleUrlAuthenticationFailureHandler {
    @Autowired
    private SecurityProperties securityProperties;
    @Autowired
    ObjectMapper objectMapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        logger.info("登录失败");
        if(LoginResponseType.JSON.equals(securityProperties.getBrowser().getLoginType())){
            log.info("securityProperties.getBrowser().getLoginType()"+securityProperties.getBrowser().getLoginType());
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(new SimpleResponse(exception.getMessage())));
        }else {
            super.onAuthenticationFailure(request, response, exception);

        }

    }
}

 

成功跳转

 

package com.aimuke.security.brewoser.anth;
/**
 *
 */

import java.io.IOException;

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

import com.imoke.security.SecurityProperties.SecurityProperties;
import com.imoke.security.SecurityProperties.properties.LoginResponseType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author 35-pxiaodong
 *
 */
@Component("imoocAuthenticationSuccessHandler")
public class ImoocAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SecurityProperties securityProperties;

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.security.web.authentication.
     * AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.
     * HttpServletRequest, javax.servlet.http.HttpServletResponse,
     * org.springframework.security.core.Authentication)
     */

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        if (LoginResponseType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
            logger.info("登录成功");

            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(authentication));
        } else {
            logger.info("有问题");
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值