标签: spring-boot

如何在Spring Security中禁用"X-Frame-Options"响应头?

我在我的jsp上有CKeditor,每当我上传一些东西时,会弹出以下错误:

 Refused to display 'http://localhost:8080/xxx/xxx/upload-image?CKEditor=text&CKEditorFuncNum=1&langCode=ru' in a frame because it set 'X-Frame-Options' to 'DENY'.
Run Code Online (Sandbox Code Playgroud)

我试过删除Spring Security,一切都像魅力一样.如何在spring security xml文件中禁用它?我应该在<http>标签之间写什么

java spring spring-security x-frame-options spring-boot

79
推荐指数
6
解决办法
11万
查看次数

Spring MVC中拦截器和过滤器的区别

我有点困惑FilterInterceptor目的.

正如我从文档中所理解的那样,Interceptor在请求之间运行.另一方面Filter,在渲染视图之前运行,但在Controller渲染响应之后.

那么postHandle()Interceptor和doFilter()Filter 之间的区别在哪里?

Spring MVC sheme 应该使用哪些用例的最佳做法是什么?在这张图片里面有作品FilterInterceptors?

java spring spring-mvc spring-boot

78
推荐指数
4
解决办法
7万
查看次数

考虑在配置中定义一个'package'类型的bean [Spring-Boot]

我收到以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.


Action:

Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.
Run Code Online (Sandbox Code Playgroud)

我之前从未见过这个错误,但@Autowire无法正常工作,这很奇怪.这是项目结构:

申请人界面

public interface Applicant {

    TApplicant findBySSN(String ssn) throws ServletException;

    void deleteByssn(String ssn) throws ServletException;

    void createApplicant(TApplicant tApplicant) throws ServletException;

    void updateApplicant(TApplicant tApplicant) throws ServletException;

    List<TApplicant> getAllApplicants() throws ServletException;
}
Run Code Online (Sandbox Code Playgroud)

ApplicantImpl

@Service
@Transactional
public class ApplicantImpl implements Applicant {

private static Log …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

78
推荐指数
11
解决办法
24万
查看次数

Spring Boot PasswordEncoder错误

我正在学习通过做一个小项目来使用Spring Boot.目前我得到了主要课程:

package com.recweb.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
/*@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})*/
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

成员类(id,firstname ..),MemberController类:

package com.recweb.springboot;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {
    @GetMapping("/members")
    public List<Member> getAllUsers() {
        return Arrays.asList(new Member(1, "amit"));
    }
}
Run Code Online (Sandbox Code Playgroud)

和WebSecurityConfig类:

package com.recweb.springboot;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager …
Run Code Online (Sandbox Code Playgroud)

spring spring-security maven spring-boot

78
推荐指数
8
解决办法
8万
查看次数

Spring Boot的application.properties值没有填充

我有一个非常简单的Spring Boot应用程序,我正在尝试使用一些外部化配置.我试图按照弹簧启动文档中的信息,但我正在遇到路障.

当我运行app.properties文件中的外部配置下面的应用程序时,不会填充bean中的变量.我确定我做的事情很愚蠢,谢谢你的任何建议.

MyBean.java(位于/ src/main/java/foo/bar /)

package foo.bar;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @Value("${some.prop}")
    private String prop;

    public MyBean() {
        System.out.println("================== " + prop + "================== ");
    }
}
Run Code Online (Sandbox Code Playgroud)

Application.java(位于/ src/main/java/foo /)

package foo;

import foo.bar.MyBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    @Autowired
    private MyBean myBean;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    } …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

77
推荐指数
4
解决办法
10万
查看次数

从@ComponentScan中排除@Component

我有一个组件,我想从@ComponentScan特定的一个组件中排除@Configuration:

@Component("foo") class Foo {
...
}
Run Code Online (Sandbox Code Playgroud)

否则,它似乎与我项目中的其他类冲突.我不完全理解碰撞,但是如果我注释掉@Component注释,事情就像我想要的那样.但是依赖这个库的其他项目希望这个类由Spring管理,所以我想在我的项目中跳过它.

我试过用@ComponentScan.Filter:

@Configuration 
@EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
  @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用.如果我尝试使用FilterType.ASSIGNABLE_TYPE,我得到一个奇怪的错误,无法加载一些看似随机的类:

引起:java.io.FileNotFoundException:类路径资源[junit/framework/TestCase.class]无法打开,因为它不存在

我也尝试使用type=FilterType.CUSTOM如下:

class ExcludeFooFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader,
            MetadataReaderFactory metadataReaderFactory) throws IOException {
        return metadataReader.getClass() == Foo.class;
    }
}

@Configuration @EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
  @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Run Code Online (Sandbox Code Playgroud)

但这似乎并没有像我想要的那样从扫描中排除组件.

我如何排除它?

java spring dependency-injection spring-ioc spring-boot

76
推荐指数
5
解决办法
12万
查看次数

Spring Boot:覆盖图标

如何覆盖Spring Boot的图标?

注意:这是我的另一个问题,它提供了另一个不涉及任何编码的解决方案:Spring Boot:是否可以在胖jar的任意目录中使用外部application.properties文件?它适用于application.properties,但也可以应用于favicon.事实上,我现在正在使用该方法进行favicon覆盖.

如果我实现了一个具有@EnableWebMvc的类,则不会加载Spring Boot的WebMvcAutoConfiguration类,并且我可以通过将其放置到静态内容的根目录来提供我自己的favicon.

否则,WebMvcAutoConfiguration会注册faviconRequestHandler bean,(参见源代码https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ web/WebMvcAutoConfiguration.java)它提供了"绿叶"图标,该图标位于Spring Boot的主资源目录中.

如何在不实现自己拥有@EnableWebMvc的类的情况下覆盖它,从而禁用Spring Boot的WebMvcAutoConfiguration类的整个默认配置功能?

此外,由于我希望在客户端(Web浏览器)端尽快更新图标文件,我想将favicon文件的缓存周期设置为0.(如下面的代码,我用于我的'static'webapp内容和脚本文件,我必须在更改文件后尽快在客户端更新.)

public void addResourceHandlers(ResourceHandlerRegistry registry)
{
    registry.addResourceHandler("/**")
        .addResourceLocations("/")
        .setCachePeriod(0);
}
Run Code Online (Sandbox Code Playgroud)

因此,只是找到保存favicon.ico文件的地方,Spring Boot的faviconRequestHandler荣誉可能还不够.

UPDATE

现在我知道我可以通过将一个favicon文件放到src/main/resources目录来覆盖默认值.但缓存期问题仍然存在.
此外,最好将favicon文件放在放置静态Web文件的目录中,而不是放在资源目录中.

UPDATE

好的,我设法覆盖了默认的一个.我做的是如下:

@Configuration
public class WebMvcConfiguration
{
    @Bean
    public WebMvcConfigurerAdapter faviconWebMvcConfiguration()
    {
        return new FaviconWebMvcConfiguration();
    }

    public class FaviconWebMvcConfiguration extends WebMvcConfigurerAdapter
    {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry)
        {
            registry.setOrder(Integer.MIN_VALUE);
            registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("/")
                .setCachePeriod(0);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,我通过调用registry.setOrder(Integer.MIN_VALUE)来添加具有最高顺序的资源处理程序来覆盖默认值.

由于Spring Boot中的默认值具有订单值(Integer.MIN_VALUE + 1),(请参阅https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/中的 FaviconConfiguration类src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java)我的处理程序获胜.

这个可以吗?还有另一种方式(比我做的更温和)?

UPDATE

这不好.当我打电话时registry.setOrder(Integer.MIN_VALUE) …

favicon spring-mvc spring-boot

75
推荐指数
4
解决办法
6万
查看次数

如何在stdout中禁用spring boot logo?

有没有办法禁用可爱但非常明显的ASCII Spring启动徽标:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.1.8.RELEASE)
Run Code Online (Sandbox Code Playgroud)

...每次运行弹簧启动应用程序时,都会在STDOUT中转储?

我在logback.xml中将所有日志记录切换为ERROR,但这没有做任何事情:

<root level="ERROR">
    <appender-ref ref="STDOUT" />
</root>
Run Code Online (Sandbox Code Playgroud)

编辑:它在文档中不称为"徽标".搜索友好的术语是一个"横幅".

java logback spring-boot

75
推荐指数
6
解决办法
3万
查看次数

Spring Boot以及如何配置MongoDB的连接细节?

作为Spring Boot的新手,我想知道如何配置MongoDB的连接细节.我尝试了正常的例子但没有覆盖连接细节.

我想指定将要使用的数据库以及运行MongoDB的主机的url/port.

任何提示或提示?

spring mongodb spring-data spring-boot

74
推荐指数
6
解决办法
12万
查看次数

Spring Boot Test忽略logging.level

我的一个maven模块在运行测试时忽略了我的日志记录级别.

在src/test/resources中我有application.properties:

app.name=bbsng-import-backend
app.description=Import Backend Module for Application
spring.profiles.active=test

# LOGGING
logging.level.root=error
logging.level.org.springframework.core =fatal
logging.level.org.springframework.beans=fatal
logging.level.org.springframework.context=fatal
logging.level.org.springframework.transaction=error
logging.level.org.springframework.test=error
logging.level.org.springframework.web=error
logging.level.org.hibernate=ERROR
Run Code Online (Sandbox Code Playgroud)

我也尝试过application-test.properties.

我的应用程序记录很多,特别是在加载上下文时.我尝试了logback.xml,logback-test.xml和logback-spring.xml,但没有任何帮助.

我的pom:

<parent>
    <groupId>at.company.bbsng</groupId>
    <artifactId>bbsng-import</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bbsng-import-backend</artifactId>
<name>bbsng-import-backend</name>

<properties>
    <start-class>at.company.bbsng.dataimport.ApplicationImportBackend</start-class>
</properties>


<dependencies>

    <!-- APPLICATION ... -->
    <dependency>
        <groupId>at.company.bbsng</groupId>
        <artifactId>bbsng-app-domain</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SPRING ... -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- JAVAX ... -->
       ...

    <!-- COMMONS ... -->
       ...

    <!-- LOMBOK ... -->
       ...

    <!-- DB -->
       ... …
Run Code Online (Sandbox Code Playgroud)

logging ignore logback spring-boot

74
推荐指数
5
解决办法
3万
查看次数