Spring Boot中具有安全性的Swagger配置示例

时间:2019-03-13 16:27:26

标签: spring-boot swagger

有人举起了弹簧靴的招摇安全性的例子吗?

我的资料夹配置是这样的:

@Bean
public Docket userApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .select()
    .apis(RequestHandlerSelectors.any())
    .paths(Predicates.not(PathSelectors.regex("/error.*")))
    .paths(PathSelectors.any())
    .build()
    .apiInfo(metaData());

}

1 个答案:

答案 0 :(得分:0)

要配置具有安全性的摇摇欲坠,您应该像这样设置securityContexts:

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    EditText userPassword;
    EditText userPin;
    EditText UserName;
    CheckBox checkBox;
    TextView Title;
    Button GoBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        UserName = findViewById(R.id.UserName);
        userPassword = findViewById(R.id.userPassword);
        userPin = findViewById(R.id.userPin);
        checkBox = findViewById(R.id.checkBox);
        GoBtn = findViewById(R.id.GoBtn);


        checkBox.setOnClickListener(this);
        // called when check box is clicked

    }

    @Override
    public void onClick(View v) {
        //method called
        if (checkBox.isChecked())
        {
            userPin.setVisibility(View.VISIBLE);
            //when checkbox is check, userPin becomes visible
        }
        else
        {
            userPin.setVisibility(View.INVISIBLE);
            //otherwise userPin is invisible
        }

        GoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View v) {
               ChangeActivityStudent();
            }
        });
    }

    private void ChangeActivityStudent(){
       Intent intent = new Intent(this, StudentActivity.class);
       ChangeActivityStudent(intent);
    }
}

编辑

我添加了fastxml同学库(private final TypeResolver typeResolver; // constructor public SwaggerConfig(TypeResolver typeResolver) { this.typeResolver = typeResolver; } @Bean public Docket apiBatch() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("path.to.package")) .paths(PathSelectors.any()) .build() .securitySchemes(Lists.newArrayList(apiKey())) .securityContexts(Collections.singletonList(securityContext())) .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(title) .description(description) .version(version) .build(); } /** * add as header the Authorization header * * @return */ private ApiKey apiKey() { return new ApiKey("apiKey", "Authorization", "header"); } /** * demand the authorization for access on /api/v1/** * * @return */ private SecurityContext securityContext() { return SecurityContext.builder().securityReferences(defaultAuth()) .forPaths(PathSelectors.regex("/api/v1.*")).build(); } private List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope( "global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return Collections.singletonList(new SecurityReference("apiKey", authorizationScopes)); } private AlternateTypeRule getAlternateTypeRule(Type sourceType, Type sourceGenericType, Type targetType, Type targetGenericType) { return AlternateTypeRules.newRule(typeResolver.resolve(sourceType, sourceGenericType), typeResolver.resolve(targetType, targetGenericType)); } )的TypeResolver属性

请注意compile group: 'com.fasterxml', name: 'classmate', version: '1.3.1'是配置类名称