问题描述
我正在学习 togglz。我的简单程序运行良好,我正在尝试添加内存中的 spring 安全用户名和密码,但我无法登录管理控制台。
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.togglz.core.manager.FeatureManager;
import org.togglz.core.util.NamedFeature;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@RestController
public class ApiTestApplication {
@Autowired
private FeatureManager featureManager;
public static void main(String[] args) {
SpringApplication.run(ApiTestApplication.class,args);
}
@GetMapping
public Map<String,Boolean> test1() {
Map<String,Boolean> enumMap = new HashMap<>();
enumMap.put("FEATURE_ONE",featureManager.isActive(new NamedFeature("FEATURE_ONE")));
enumMap.put("FEATURE_TWO",featureManager.isActive(new NamedFeature("FEATURE_TWO")));
return enumMap;
}
}
应用属性
togglz.console.feature-admin-authority=ADMIN
togglz.features.FEATURE_ONE.enabled=false
togglz.features.FEATURE_TWO.enabled=false
togglz.console.enabled=true
togglz.console.path=/togglz-console
togglz.console.secured=true
management.endpoints.web.exposure.include=*
logging.level.org.togglz=DEBUG
安全等级
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.csrf()
.disable()
.formLogin()
.and()
.logout();
// @@formatter:on
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth.inMemoryAuthentication()
.withUser("admin")
.password("{noop}admin")
.roles("ADMIN");
}
}
构建gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.togglz:togglz-spring-boot-starter:+'
implementation 'org.togglz:togglz-console:+'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.togglz:togglz-spring-security:+'
implementation 'org.springframework.security:spring-security-test'
}
test {
useJUnitPlatform()
}
http://localhost:8080/ 带有 un/pass -admin/admin 我能够看到布尔值。 但要切换它, http://localhost:8080/togglz-console/index 是说 type=Forbidden,status=403
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)