Skip to content

Commit b1ae81a

Browse files
author
leijiankun
committed
Check current user authentication
1 parent 55c0876 commit b1ae81a

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ I recommend you import the source code into Intellij IDE to edit the code.
7777
3. Open the project in Intellij IDEA.
7878
4. Run `SpringBlogApplication.java` as Java application.
7979
5. Preview: http://localhost:8080
80-
Admin: http://localhost:900/admin , the default admin account is: admin@admin.com, password: admin
80+
Admin: http://localhost:8080/admin , the default admin account is: `admin`, password: `admin`
8181

8282

8383
> Lombok is required to run the project. You can install the plugin in Intellij IDEA.
@@ -91,9 +91,10 @@ I recommend you import the source code into Intellij IDE to edit the code.
9191
- Run it (Java8 is a must)
9292

9393
```
94-
# assuming you have the jar and yml files under current dir
94+
java -jar SpringBlog-0.1.jar --spring.profiles.active=prod
95+
# OR with external spring profile file
9596
java -jar SpringBlog-0.1.jar --spring.config.location=application-production.yml
9697
```
9798

9899
## License
99-
Modified BSD license. Copyright (c) 2015, Jiankun LEI (Raysmond).
100+
Modified BSD license. Copyright (c) 2015 - 2018, Jiankun LEI (Raysmond).

src/main/java/com/raysmond/blog/support/web/SyntaxHighlightService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* @author Raysmond
55
*/
66
public interface SyntaxHighlightService {
7-
public String highlight(String content);
7+
String highlight(String content);
88
}

src/main/java/com/raysmond/blog/support/web/ViewHelper.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.raysmond.blog.support.web;
22

3-
43
import com.domingosuarez.boot.autoconfigure.jade4j.JadeHelper;
54
import com.raysmond.blog.services.AppSetting;
5+
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.security.core.context.SecurityContextHolder;
710
import org.springframework.stereotype.Service;
811

912
import java.text.SimpleDateFormat;
@@ -24,6 +27,18 @@ public class ViewHelper {
2427
private String applicationEnv;
2528
private long startTime;
2629

30+
/**
31+
* Check if current user is authenticated
32+
*
33+
* @return true/false
34+
*/
35+
public boolean isLogin() {
36+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
37+
return null != authentication
38+
&& authentication.isAuthenticated()
39+
&& !(authentication instanceof AnonymousAuthenticationToken);
40+
}
41+
2742
@Autowired
2843
public ViewHelper(AppSetting appSetting) {
2944
this.appSetting = appSetting;

src/main/resources/templates/layout/navbar.jade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ nav.navbar.navbar-bright
88
li: a(href="/tags") Tags
99
li: a(href="/posts/projects") Projects
1010
li: a(href="/about") About
11+
if viewHelper.isLogin()
12+
li: a(href="/admin") Administration
1113

0 commit comments

Comments
 (0)