2016-05-21 5 views
0

私はウェブで解決策を探しましたが、これはわかりません。java - ログアウトしたユーザーのみにログアウトボタンを表示

私はMavenでSpring Bootを使用していて、私のpom.xmlファイルには
spring-boot-starter-thymeleafの依存関係があります。

私は、ユーザーがログインしている場合にのみ、Thymeleafを使用して、ログアウトボタンを表示しようとしているが、このコードは動作していないよう:

:それは、このエラーを示す続け

<div th:if="${#authorization.expression('isAuthenticated()')}"> 
    <div class="navbar-form navbar-right"> 
     <a href="/logout"> 
      <button type="text" class="btn btn-primary navbar-right">Logout</button>  
     </a> 
    </div> 
</div> 

There was an unexpected error (type=Internal Server Error, status=500). 
Exception evaluating SpringEL expression: "#authorization.expression('isAuthenticated()')" 

と、私がthymeleafで「if」条件を持っている行番号。

どうすれば解決できますか?

答えて

0

sec:authorize="isAuthenticated()"を使用できますが、これには依存関係などを追加する必要があります。また

<dependency> 
    <groupId>org.thymeleaf.extras</groupId> 
    <artifactId>thymeleaf-extras-springsecurity4</artifactId> 
    <version>2.1.2.RELEASE</version> 
    <scope>compile</scope> 
</dependency> 

、にisAuthenticatedおよび他の類似の表現は春で評価することができるようにSpringSecurityDialectを追加してください:私が使用していました。例:HTMLページ自体へ

Bean 
public SpringTemplateEngine templateEngine() { 
    SpringTemplateEngine engine = new SpringTemplateEngine(); 
    engine.setTemplateResolver(templateResolver()); 
    engine.addDialect(new SpringSecurityDialect()); 
    return engine; 
} 

は、sec:タグが認識されていることを確認するために<html>タグに次の行を追加します。

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" 
+0

ありがとう:

<div th:if="${currentuser} == 'noBodyHome'"> your regular bar </div> 

にログイン!私はspringsecurity3を使用していたので、isAuthenticated()メソッドは機能しませんでした。 – Alex

0

これは非正統的であるかもしれません。あなたの建設的な批判を自由に感じてください。プリンシパルを使用してコントローラ内の変数を設定してから、あなたのthymeleafでheader/nav barにif/elseを設定することができます。あなたのナビゲーションバーで

@RequestMapping(value = "/mypage", method = RequestMethod.GET) 
public String mypagemethod(Principal prin, Model model) 
{ 
    if(prin == null) 
    { 
     model.addAttribute("currentuser", "noBodyHome"); 
    } 
    else 
    { 
     model.addAttribute("currentuser", prin.getName()); 
    } 
    return "mypage"; 
} 

は:

<div th:if="${currentuser} != 'noBodyHome'"> show prin + show logout button</div> 
+0

私はこのタイプの問題にプリンシパルを使用していません。私はisAuthenticated()メソッドで解決策を見ましたが、pom.xmlに間違った依存関係を使用しました – Alex

関連する問題