zwzw1219 2017-10-15 17:32:58 +08:00
parent 9297562a5c
commit 4dd1341615
3 changed files with 20 additions and 11 deletions

View File

@ -2,15 +2,16 @@ package com.boot.security.server.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 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.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.AuthenticationFailureHandler; import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.boot.security.server.filter.TokenFilter; import com.boot.security.server.filter.TokenFilter;
@ -37,19 +38,23 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(tokenFilter, FilterSecurityInterceptor.class); http.csrf().disable();
// 基于token所以不需要session // 基于token所以不需要session
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests() http.authorizeRequests()
.antMatchers("/login.html", "/static/**", "/statics/**", "/v2/api-docs/**", "/swagger-resources/**", .antMatchers(HttpMethod.GET, "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js",
"/swagger-ui.html", "/webjars/**") "/fonts/**", "/layui/**", "/img/**", "/v2/api-docs/**", "/swagger-resources/**", "/webjars/**")
.permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login.html") .permitAll().anyRequest().authenticated();
.loginProcessingUrl("/login").successHandler(authenticationSuccessHandler) http.formLogin().loginPage("/login.html").loginProcessingUrl("/login")
.failureHandler(authenticationFailureHandler).and().logout().logoutUrl("/logout") .successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler);
.logoutSuccessHandler(logoutSuccessHandler); http.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
http.csrf().disable(); // 解决不允许显示在iframe的问题
http.headers().frameOptions().sameOrigin(); http.headers().frameOptions().disable();
http.headers().cacheControl(); http.headers().cacheControl();
http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
} }
@Override @Override

View File

@ -11,6 +11,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import com.boot.security.server.dto.LoginUser; import com.boot.security.server.dto.LoginUser;
@ -23,7 +24,7 @@ import com.boot.security.server.service.TokenService;
* *
* 20171014 * 20171014
*/ */
//@Component @Component
public class TokenFilter extends OncePerRequestFilter { public class TokenFilter extends OncePerRequestFilter {
private static final String TOKEN_KEY = "token"; private static final String TOKEN_KEY = "token";

View File

@ -1,5 +1,8 @@
$.ajaxSetup({ $.ajaxSetup({
cache : false, cache : false,
headers : {
"token" : localStorage.getItem("token")
},
error : function(xhr, textStatus, errorThrown) { error : function(xhr, textStatus, errorThrown) {
var msg = xhr.responseText; var msg = xhr.responseText;
var response = JSON.parse(msg); var response = JSON.parse(msg);