前后端添加ba认证
博客专区 > go4it 的博客 > 博客详情
前后端添加ba认证
go4it 发表于1年前
前后端添加ba认证
  • 发表于 1年前
  • 阅读 13
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

##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

标签: Spring Boot
共有 人打赏支持
粉丝 15
博文 425
码字总数 213112
×
go4it
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: