脚本宝典收集整理的这篇文章主要介绍了前后端添加ba认证,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
sPRing securITy
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
config
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers("/**").authenticated()
.anyRequest().anonymous()
.and()
.httpBasic()
.realmName("known");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("xixicat").password("xixicat").roles("USER");
}
}
jquery配置
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa('xixicat' + ":" + 'xixicat'));
},
url: '/demo',
type: 'POST',
dataType:"json",
contentType:"application/json",
data:JSON.stringify(saveData),
success: function (res, status) {
window.location.reload();
},
error: function (data, status) {
if (data.status == 200) {
window.location.reload();
}else{
dangerDialog(data.statusText);
}
}
});
android的retrofit配置
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().clear();
httpClient.interceptors().add(new Interceptor() {
@override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", basic)
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
Gson gson = builder.create();
this.retrofit = new Retrofit.Builder()
.baseUrl(API)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
docs
以上是脚本宝典为你收集整理的前后端添加ba认证全部内容,希望文章能够帮你解决前后端添加ba认证所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。