分类 Spring 下的文章

1.Get the User in a Bean

The simplest way to retrieve the currently authenticated principal is via a static call to the SecurityContextHolder:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();

An improvement to this snippet is first checking if there is an authenticated user before trying to access it:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
    String currentUserName = authentication.getName();
    return currentUserName;
}

- 阅读剩余部分 -

Here is the difference:

  • addFlashAttribute() actually stores the attributes in a flashmap (which is internally maintained in the users session and removed once the next redirected request gets fulfilled)
  • addAttribute() essentially constructs request parameters out of your attributes and redirects to the desired page with the request parameters.

So the advantage of addFlashAttribute() will be that you can store pretty much any object in your flash attribute (as it is not serialized into request params at all, but maintained as an object), whereas with addAttribute() since the object that you add gets transformed to a normal request param, you are pretty limited to the object types like String or primitives.

answerd by Biju Kunjummen https://stackoverflow.com/questions/14470111/spring-redirectattributes-addattribute-vs-addflashattribute

denis-anisimov:

This looks like a bug in Spring: spring-projects/spring-boot#27147

JS WebSocket class can't connect to the server and there is nothing to fix on our side.
The same happens with:

  • console code ws = new WebSocket("ws://localhost: 35729");
  • Live reload Chrome extension: it doesn't work with 2.5.2

pleku:
Fixed in Spring Boot 2.4.9 & 2.5.3

Live reload is broken starting with Spring Boot 2.5.2 #11375
DevTools' LiveReload support's HTTP header handling is case sensitive #26813

    <mvc:annotation-driven/>
...
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
                </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            </list>
        </property>
    </bean>

此时配置,response content-type 依然是 text/plain;charset=ISO-8859-1

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg>
                    <util:constant static-field="java.nio.charset.StandardCharsets.UTF_8"/>
                </constructor-arg>
                <property name="writeAcceptCharset" value="false" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

<mvc:annotation-driven> 配置存在时,bean StringHttpMessageConverter 放于 <mvc:message-converters> 中, response 才能正确返回 text/plain;charset=UTF-8


声明 util:
<beans ...
     xmlns:util="http://www.springframework.org/schema/util"
...
 xsi:schemaLocation="...
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-3.1.xsd
...">
xml: 注入静态成员变量至 spring bean
https://www.cnblogs.com/yzx2018/p/8884141.html

As an answer to okutane, please see snippet:

@JoinColumn(name = "car_id", insertable = false, updatable = false)
@ManyToOne(targetEntity = Car.class, fetch = FetchType.EAGER)
private Car car;

@Column(name = "car_id")
private Long carId;

So what happens here is that when you want to do an insert/update, you only populate the carId field and perform the insert/update. Since the car field is non-insertable and non-updatable Hibernate will not complain about this and since in your database model you would only populate your car_id as a foreign key anyway this is enough at this point (and your foreign key relationship on the database will ensure your data integrity). Now when you fetch your entity the car field will be populated by Hibernate giving you the flexibility where only your parent gets fetched when it needs to.

answered by 'Jonck van der Kogel'

JPA many-to-one relation - need to save only Id