提交 cec4e167 编写于 作者: 江南一点雨

优化一个登录问题

上级 5fe7bbb4
......@@ -9,6 +9,7 @@ import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
......@@ -30,6 +31,7 @@ import java.io.PrintWriter;
* Created by sang on 2017/12/28.
*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
......@@ -61,7 +63,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
o.setAccessDecisionManager(urlAccessDecisionManager);
return o;
}
}).and().formLogin().loginPage("/index.html").loginProcessingUrl("/login").usernameParameter("username").passwordParameter("password").permitAll().failureHandler(new AuthenticationFailureHandler() {
}).and().formLogin().loginPage("/login_p").loginProcessingUrl("/login").usernameParameter("username").passwordParameter("password").permitAll().failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=utf-8");
......
package org.sang.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by sang on 2017/12/29.
*/
@RestController
public class RegLoginController {
// @RequestMapping("/login_p")
// public void login(HttpServletResponse resp) throws IOException {
// resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// resp.setContentType("application/json;charset=UTF-8");
// PrintWriter out = resp.getWriter();
// out.write("{\"status\":\"error\",\"msg\":\"尚未登录,请登录!\"}");
// out.flush();
// out.close();
// // return new RespBean("error", "尚未登录,请登录!");
// }
@RequestMapping("/login_p")
public void login(HttpServletResponse resp) throws IOException {
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
resp.setContentType("application/json;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.write("{\"status\":\"error\",\"msg\":\"尚未登录,请登录!\"}");
out.flush();
out.close();
// return new RespBean("error", "尚未登录,请登录!");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册