我正在尝试为我的Spring Boot应用程序添加安全性.我当前的应用程序是使用REST控制器,每次我收到GET或POST请求时,我都会读取HTTP标头以检索用户和密码,以便根据我存储了所有用户的属性文件验证它们.我想将此更改为使用Spring Security,这是我到目前为止所得到的:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/index.html").permitAll()
.antMatchers("/swagger-ui.html").hasRole("ADMIN")
.anyRequest().authenticated();
}
@Bean
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("admin").password("password").roles("ADMIN").build());
}
}
Run Code Online (Sandbox Code Playgroud)
如何告诉configure方法从头部而不是登录表单中检索用户凭据?
security authentication spring-security http-headers spring-boot
我想为我的Java 8 Maven项目创建代码覆盖率报告.我使用Cobertura时遇到问题,因为它无法处理Java 8语法.有熟悉解决方法的人吗?任何其他Maven插件?
是否有必要保护JAX-RS请求免受CSRF的侵害?
根据定义, REST是无状态的,因此不存在会话ID(会话cookie),因为根本没有会话(另请参阅/sf/answers/1102264761/).
我的Spring Security Java配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Configuration
@Order(1)
public static class JaxRsWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.antMatcher("/services/**")
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/services/**").permitAll()
.anyRequest().hasAuthority("ROLE_user")
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我发现了以下博客:无状态弹簧安全第1部分:无状态CSRF保护.不幸的是,博客没有解释,为什么需要CSRF保护.
没有会话cookie,还有其他CSRF攻击吗?
我有一个运行基于 Spring 的 servlet 的 Tomcat 服务器。
我已经设置了[project root]/src/log4j.properties如下文件:
# Root logger option
log4j.rootLogger=WARN, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss} %m [%c{3}:%L]%n
log4j.logger.com.martincarney.bugTracker=DEBUG
log4j.logger.com.martincarney.bugTracker.controller=ERROR
Run Code Online (Sandbox Code Playgroud)
这正确地记录了我自己的代码,但似乎对我正在使用的各种库中的记录没有任何影响。例如,org.apache.*即使我添加log4j.logger.org.apache=WARN到我的 log4j.properties 中,我仍然会在 Tomcat 启动期间从Eclipse 控制台错误流中获取 INFO 日志。
我正在使用slf4j-api和slf4j-log4jjars,通过 Maven 获得。
如何在自己的代码之外控制日志记录级别和目标?
var arr = [{
value: 'a'
}];
var getTest = function() {
jQuery.each(arr, function(i, val) {
if (val.value == "a") {
return val;
}
});
}
alert(getTest().value);
Run Code Online (Sandbox Code Playgroud)
jquery-3.1.0.js:3793 Uncaught TypeError: 无法读取未定义的属性“值”
是有可能trim()的方法是在两个方面数据绑定,从XML获取信息时的工作.如果有,怎么样?
android:text='@={contact.contactDetails.name}'
Run Code Online (Sandbox Code Playgroud) 我正在使用Maven JGit-Flow插件来自动化一些发布过程。不幸的是,当我尝试使用mvn jgitflow:release-start以下命令启动新版本时遇到了这个问题:
[ERROR] Failed to execute goal external.atlassian.jgitflow:jgitflow-maven-plugin:1.0-m5.1:release-start (default-cli) on project <myProjectName>: Error starting release: Error starting release: Working tree has untracked files
Run Code Online (Sandbox Code Playgroud)
但是我在这里(在主服务器上)也看不到和未跟踪的文件:
git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)
知道Maven JGit-Flow插件如何查找未跟踪的文件吗?
我想知道两个音频文件是否相同或者一个包含另一个。
为此,我使用音乐指纹
byte[] firstAudio = readAudioFileData("first.mp3");
byte[] secondAudio = readAudioFileData("second.mp3");
FingerprintSimilarityComputer fingerprint =
new FingerprintSimilarityComputer(firstAudio, secondAudio);
FingerprintSimilarity fingerprintSimilarity = fingerprint.getFingerprintsSimilarity();
System.out.println("clip is found at " + fingerprintSimilarity.getScore());
Run Code Online (Sandbox Code Playgroud)
要将音频转换为字节数组,我使用声音 API
public static byte[] readAudioFileData(final String filePath) {
byte[] data = null;
try {
final ByteArrayOutputStream baout = new ByteArrayOutputStream();
final File file = new File(filePath);
final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
byte[] buffer = new byte[4096];
int c;
while ((c = audioInputStream.read(buffer, 0, buffer.length)) != -1) {
baout.write(buffer, 0, c);
} …Run Code Online (Sandbox Code Playgroud) 我想使用 Yii 1.x 获取我当前的页面 URL。
这是我当前的页面:
http://example.com/abc/def
Run Code Online (Sandbox Code Playgroud)
我想获得完整的 URL 或最后一个参数。
迁移到 Spring Boot 3 后,
extends ResourceServerConfigurerAdapter is deprecrated.
Run Code Online (Sandbox Code Playgroud)
因此,无法使用覆盖的方法
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
Run Code Online (Sandbox Code Playgroud)
我可以将其替换为
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeHttpRequests(auth->auth.
requestMatchers(whitelistedEndPoints()).permitAll()
.anyRequest()
.anonymous())
.httpBasic().disable();
return http.build();
}
Run Code Online (Sandbox Code Playgroud)
我有一个现有代码可以从 OAuth 获取 jwt 令牌,如下所示
public String getJwtTokenFromAuth() {
OAuth2Authentication auth =(OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
OAuth2AuthenticationDetails oauth2AuthenticationDetails = (OAuth2AuthenticationDetails) auth.getDetails();
return oauth2AuthenticationDetails.getTokenValue();
}
Run Code Online (Sandbox Code Playgroud)
然而,
OAuth2Authentication 和 OAuth2AuthenticationDetails 不可用
我如何用 Spring Boot 3 的新 Spring Security 模块替换此代码/功能。下面是 pom 依赖项,请建议我是否需要添加/删除任何依赖项?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)