LoginSignup
19
22

More than 5 years have passed since last update.

Spring BootのSpring SecurityでBasic認証のID・パスワードを変更する

Posted at

ちょっとだけハマったのでメモ。

Spring Bootでは以下の依存関係を追加するだけでSpring Securityを利用出来て、デフォルトでBasic認証がかかるようになる。

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

この時のデフォルトIDは「user」、パスワードは以下のようにSpring Boot起動時のコンソールにはかれているので、それをコピって利用すればOK。

抜粋.
Using default security password: 2ea51f3a-36a6-473c-951d-6b73d9ee6b50

で、このIDとパスワードを変更する時は、application.ymlもしくはapplication.propertiesに以下のような設定を入れる(以下のnagaokaとしているところを任意のIDとパスワードに変更)。

application.yml
security:
  user:
    name: nagaoka
    password: nagaoka
application.properties
security.user.name=nagaoka
security.user.password=nagaoka

ちなみに、このように変更するとコンソールにはデフォルトパスワードは出力されなくなる。
ただし、以下のようにIDのみ変更を入れた場合はデフォルトパスワードが利用されるのでコンソールに出力される。

application.yml
security:
  user:
    name: nagaoka
application.properties
security.user.name=nagaoka

参考にしたサイト
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security
※公式サイト

19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22